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
How to Define a List Bean in Spring
Booleans, Conditional Operators and Autoboxing
Why Does Java Have an "Unreachable Statement" Compiler Error
Is There a Good Natural Language Processing Library
How to Remove Special Characters from a String
Is It Bad to Explicitly Compare Against Boolean Constants E.G. If (B == False) in Java
What Is a Difference Between <? Super E> and <? Extends E>
Windowbuilder Design Tab Incomplete in Eclipse
How to Remove Entity with Manytomany Relationship in JPA (And Corresponding Join Table Rows)
How to Create Table Using Ascii in a Console
Ignore Fields from Java Object Dynamically While Sending as JSON from Spring MVC
Lambda Expression and Generic Defined Only in Method
Why Do I Get the "Unhandled Exception Type Ioexception"
How to Go About Formatting 1200 to 1.2K in Java
Spring MVC - How to Get All Request Params in a Map in Spring Controller