0

I'm exercising static keyword. I've declared a static method whose return type is class. when I access this method from main method is gives me following error. How can I return the object from this method?

error: non-static variable this cannot be referenced from a static context
            return this;

Following is my code

public class StaticKeyword{

   public static StaticKeyword run(){
     return this;
   }

   public static void main(String args[]){
     System.out.println(StaticKeyword.run());
   }    
}
RBT
  • 24,161
  • 21
  • 159
  • 240
Ashwini Pandey
  • 115
  • 2
  • 2
  • 9
  • You can't use `this` in a static context. What's your `run()` method supposed to return? (hint: create a new instance: `return new StaticKeyword()`, or make `StaticKeyword` a singleton with a static instance) – ernest_k Jul 17 '18 at 17:39
  • Static methods are not part of any class instance, so referring to _this_ makes no sense inside them. Imagine calling of StaticKeyword.run() method from some other class. What should _this_ mean in that context ? – Dragan Jovanović Jul 17 '18 at 17:42
  • The whole point of `static` is that there is no `this`. Static methods are called on the class itself, and not an instance (they can be called on an instance, but that's not what you're doing). – mypetlion Jul 17 '18 at 17:43
  • it means static method never contains this keyword ? – Ashwini Pandey Jul 17 '18 at 17:47
  • yep, no `this` in `static` methods. Never. – Bublik Jul 17 '18 at 17:49

3 Answers3

1

A static method or a static variable belongs to a class and not the instance of the class. this is an instance variable which points to the current reference.

Hence this cannot be used within a static block. So, you should rephrase your code something like this,

public static class StaticKeyword {

    public static StaticKeyword run(){
        return new StaticKeyword();
    }

    public static void main(String args[]){
        System.out.println(StaticKeyword.run());
    }    
}

Also keep in mind that a method which is declared as static would remain in main memory forever (ie' until the java process stops). Unless and until you would use this method very frequently something like a util classes and methods could be made as static

  1. For each access without creating an object
  2. For faster access - since it is static the method would be already there in main memory during the consecutive method calls.

When you do not use the method frequently it is always good to go with accessing the method by creating an instance to the corresponding class.

Tom Taylor
  • 3,344
  • 2
  • 38
  • 63
0

You need to change it to this:

public static class StaticKeyword {

    public static StaticKeyword run(){
        StaticKeyword returnObject = new StaticKeyword();
        return returnObject;
    }

    public static void main(String args[]){
        System.out.println(StaticKeyword.run());
    }    
}
Bill Baits
  • 82
  • 9
0

this is a reference to the current object — the object whose method or constructor is being called. However no instance is created to be returned. So you will need to do something like return new StaticKeyword()

also tip: I personally struggled when I was learning the keyword static. It is a good rule of thumb to ask yourself "Do I want to call this method even if there is no instance of this Obj exist?" If so then your method should be static.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
OLIVER.KOO
  • 5,654
  • 3
  • 30
  • 62