Why Can't We Use 'This' Keyword in a Static Method

Why can't we use 'this' keyword in a static method

Because this refers to the object instance. There is no object instance in a call of a static method. But of course you can access your static field (only the static ones!). Just use

class Sub {
static int y;
public static void foo() {
y = 10;
}
}

If you want to make sure you get the static field y and not some local variable with the same name, use the class name to specify:

class Sub {
static int y;
public static void foo(int y) {
Sub.y = y;
}
}

Why keyword 'this' cannot be used in a static method?

Because this points to an instance of the class, in the static method you don't have an instance.

The this keyword refers to the current instance of the class. Static member functions do not have a this pointer

You'll notice the definition of a static member is

Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object

Which is why this has nothing to point to.

Why can't you use the keyword 'this' in a static method in .Net?

That's an easy one. The keyword 'this' returns a reference to the current instance of the class containing it. Static methods (or any static member) do not belong to a particular instance. They exist without creating an instance of the class. There is a much more in depth explanation of what static members are and why/when to use them in the MSDN docs.

Why keyword this work with static methods?

this in JS is not the same as this in the likes of an actual OO language like Java etc. Where this in the likes of java refers to the current object of a class, in JS there are no 'classes', this in JS just refers to a current context scope, which may be, in your case, a 'static' method on a 'class', which will essentially boil down to being the current scope of a plain old object

this: Cannot use this in static context

See, "this" keyword refers to current object due to which method is under exceution. As, you cannot call static method using instance of class. That is why, "this" can't be used in the above example in a static method as it is trying to print current instance wich is not at all created. So, I think thats why there is an compile time error that you are getting.

How to use this keyword in a static method in java?

You can create a static method with one input parameter that is the Class you need to use.

For example:

public static void showMyTouch(MyActivity act, String message){
Toast.makeText(act, message, Toast.LENGTH_LONG).show();
}

Cannot use 'this' in static context

You have defined a static method here :

  public static long addContact(String name, String email)

Static methods and class variables are tied to the Class and not to any specific instance of the Class. You cannot use the this keyword inside it as it refers to the current instance. One of the choice will be to declare the method as instance method removing the static keyword from the method declaration, if indeed the method logic depends on the state of the current instance.

I believe the problem in using this inside a static method will be that during runtime if your code calls the static method as ClassName.staticMethodName() , the runtime will have no idea how to resolve this in this context.



Related Topics



Leave a reply



Submit