Resolve Cannot make a static reference to the non-static method error

 1,109 total views,  4 views today

In this post, I am going to help you to resolve cannot make a static reference to the non-static method error.

First let us seen example where we get this error.

public class Test
{

    public static void main(String[] args)
    {
        run();
    }

    void run()
    {
        System.out.println("I am running");
    }
}

The above code will throw a compilation error at the main method stating “Cannot make a static reference to the non-static method”.

Before understanding the error, let us first learn a few basics in java.

What is a non-static method?

  1. Non-static method belongs to the instance of the class. It is also called as instance methods.
  2. Non-static methods or instance methods can be called or invoked only by the instance of the class
  3. Non-static methods can be invoked only from the other non-static methods.

This also rules apply to instance variables also.

Example

public class StaticReference
{
    void run()
    {
        System.out.println("I am running");
    }
}

//To acces  the run().
StaticReference test = new StaticReference();
test.run();

In the above example, we have created an instance for StaticReference and invoked the run method using the instance.

What is a static method?

  1. Static methods belong to the class itself.
  2. We can call the method using the class Name
  3. There is no need to create an instance for the class to access the static method.

This is all rules applies for static variables also.

Example

public class StaticReference
{
    static void run()
    {
        System.out.println("I am running");
    }
}
//To acces  the run().
StaticReference.run();

In the above example, we have invoked the run() using the class name.

Can we call a static method inside an instance method?

The answer is Yes.

Example

void run()
{
    walk();
    System.out.println("I am running");
    
}

static void walk()
{
    System.out.println("I am walking");
}

Can we call instance method inside the static method?

The answer is No. A static method should only be accessed in Static way i.e., using the class name.
Else we will get Cannot make a static reference to the non-static method error

Example

public class StaticReference
{

     void run()
    {
        System.out.println("I am running");
    }
    
    static void walk()
    {
        run(); //We cannot call like this.
		StaticReference test = new StaticReference();//We have to call using instance of the class
        test.run();
        System.out.println("I am walking");
    }
}

So how to fix this error?

Only way is to create the instance and invoke the static method.

Thanks for reading.Please leave your comments and give us a thumbs up!

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.