Monday, 15 June 2015

Static reference to the non-static method or field error

When a person moves beyond the "Hello world" program where everything is with in a main method and writes a program with more than one method, one error he or she surely will encounter is "Cannot make a static reference to the non-static method or a non-static field".
Not only beginners even experienced programmers unknowingly keep making the mistake of trying to access a non-static method or field from a static context and get a reminder from compiler that they have forgotten the basics! At least I do that many times when I write a program to test something and from main method (which is static) I try to access some non-static method.

static ref error

Here it can be seen how the compiler complains about trying to access a non-static variable and a non-static method from the static main method.

So the question is why non-static method or field can't be called from a static method?

Any static field or method is associated with the class, rather than with any object.
A variable declared as static is associated with the class rather than with any object. When objects of its class are created, copy of static variable is not created per object. All objects of the class share the same static variable. Same as static variable, static method also is associated with the class rather than objects of the class.

On the other hand instance variables are associated with objects they will come into existence only when an object of the class is created and they may have different values for different objects. Same thing with non-static method they are also associated with the object thus they will come into existence only when an object of the class is created.

Now we can't access anything which doesn't even exist at the time of calling, can we?

Static methods or fields can be accessed without even creating an instance of the class, we just need to qualify the static member with the class name and access it. But non-static members need instance of the class to be created to come into existence. That is the reason why non-static field or method can not be called from the static context.

The obvious solution to get rid of this error is to create an instance of the class and then access the non-static members.

public class StaticDemo {        
    int counter = 0;
    public static void main(String args[]){  
        System.out.println(" in main method ");
        StaticDemo sd = new StaticDemo();
        sd.counter = 1;
        sd.displayCount();
        
    }
    
    public void displayCount(){
        System.out.println("" + counter);
    }
}

That's all for this topic Static reference to the non-static method or field error. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. static in Java
  2. static method overloading or overriding in Java
  3. static import in Java
  4. Why main method static in Java
  5. interface static methods in Java 8
  6. static block in Java

You may also like -

>>>Go to Java Basics page

No comments:

Post a Comment