Monday, February 22, 2021

Fix Cannot make a static Reference to The Non-static Method 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 and get the error "Cannot make a static reference to the non-static method or a non-static field".

static reference to the non-static 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? in Java.

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 fix "Cannot make a static reference to the non-static method or a non-static field" error in Java 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 Fix Cannot make a static Reference to The Non-static Method Error. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related Topics

  1. Why main Method static in Java
  2. static Method Overloading or Overriding in Java
  3. static Import in Java With Examples
  4. Interface Static Methods in Java 8
  5. Core Java Basics Interview Questions And Answers

You may also like-

  1. super Keyword in Java With Examples
  2. this Keyword in Java With Examples
  3. Constructor Overloading in Java
  4. Inheritance in Java
  5. Difference between abstract class and interface
  6. Functional interface annotation in Java 8
  7. final Vs finally Vs finalize in Java
  8. Race Condition in Java Multi-Threading

No comments:

Post a Comment