Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

this: Cannot use this in static context

Tags:

java

this

Can you please help me with below code. The error is: "Cannot use This in a static context"

public class Sample2 {
    /**
     * @param args
     */
    public static void main(String[] args) 
    {
        Sample2 sam=new Sample2();  

        //Below code works fine
        System.out.println(sam);

        //Below code is displaying error
        System.out.println(this);
    }
}
like image 596
Cyborgz Avatar asked May 01 '13 09:05

Cyborgz


People also ask

How do you resolve Cannot use this in a static context?

In java you can not use this in static methods (static context). Static methods do not point to any instance of the enclosing class. Show activity on this post. If we try to access this from a static context , compiler has no way to guess which instance, you are referring too.

Can we use this in static context?

But static contexts(methods and blocks) doesn't have any instance they belong to the class. In a simple sense, to use “this” the method should be invoked by an object, which is not always necessary with static methods. Therefore, you cannot use this keyword from a static method.

Why can't we use this inside static context?

No, we can't use “this” keyword inside a static method. “this” refers to current instance of the class. But if we define a method as static , class instance will not have access to it, only CLR executes that block of code. Hence we can't use “this” keyword inside static method.

What do Cannot be referenced from a static context?

For the same reasons, a non-static method cannot be referenced from a static context, either, as the compiler cannot tell which particular object the non-static member belongs to.


4 Answers

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.

like image 140
Priyanka Vishwakarma Avatar answered Oct 14 '22 07:10

Priyanka Vishwakarma


They keyword this refers to the instance of the class. In a static context, you have no instance, therefore you can't refer it.

For more information, refer to this answer: What is the meaning of "this" in Java?

like image 29
Matten Avatar answered Oct 14 '22 08:10

Matten


In java you can not use this in static methods (static context).

Static methods do not point to any instance of the enclosing class.

A static method cannot refer to “this” or “super” keywords in anyway

Refer official docs on this keyword

like image 41
Suresh Atta Avatar answered Oct 14 '22 07:10

Suresh Atta


If we try to access this from a static context , compiler has no way to guess which instance, you are referring too. main is a static method here.

like image 39
Vivek Vermani Avatar answered Oct 14 '22 07:10

Vivek Vermani