Articles

Cannot make a static reference to the non-static method in java

Cannot make a static reference to the non-static method in java

'Cannot make a static reference to the non-static method' - is a compile time error thrown in java.

 

Lets learn why we get the above compile time error in java?

And how to solve it.


We get above compiler error in two cases:

  1. When we try to call non-static method from within a static method.
  2. When we try to access non-static field or variable from within a static method.

Lets see both one by one and how to solve it.


Case I: Calling non-static method from within static method

We will understand the concept with the help of below code:

public class StaticRefDemo {

	public static void main(String[] args) {
		sayHello(); //compiler error
	}
	
	//implementation of sayHello() method
	public void sayHello(){
		System.out.println("Hello Java");
	}

}

Now if we write above code, then:

  • sayHello() when called from within main() method will throw compile time error - 'Cannot make a static reference to the non-static method sayHello() from the type StaticRefDemo'
  • If you are using eclipse and hover mouse over the sayHello(); you will also get suggestion - 'change sayHello() to static'

In java, we are not allowed to make a call to non-static method from within the static method.

In above code, we are calling method sayHello() which is non-static from the main() method which is static (notice the keyword static in public static void main). Thus we didn't follow the rule set by java hence got compile time error.

What we can do?

While providing implementation for sayHello() method in above class add keyword static as shown below:

public class StaticRefDemo {

	public static void main(String[] args) {
		sayHello();
	}
	
	//implementation of sayHello() method
	public static void sayHello(){
		System.out.println("Hello Java");
	}

}

OUTPUT

Hello Java

 

Our code executed successfully and produced desired output.

 

Thus don't try to call non-static method from within a static method to avoid above compiler error.


Case II: Access non-static variables from static method

Lets understand the above scenario with

public class StaticRefDemo {

	int var = 1;
	public static void main(String[] args) {
		sayHello();
	}
	
	//implementation of sayHello() method
	public static void sayHello(){
		System.out.println("Print val: "+ var);
	}

}

Now if we write above code, then:

  • When we try to access and print the variable var in sayHello() then the compiler error will occur - 'Cannot make a static reference to the non-static field var
  • If you are using eclipse, then try to hover your mouse on var whithin   System.out.println("Print val: "+ var);  and you will see above error highlighted and suggestion change var to static.

In java, we are not allowed to access non-static field from within the static method.

In above code, we are accessing and trying to print 'var' which is non-static integer variable inside the static method sayHello(). Thus we didn't follow the rule set by java hence got compile time error.

What we can do?

Make variable var static as shown below:

public class StaticRefDemo {

	static int var = 1;
	public static void main(String[] args) {
		sayHello();
	}
	
	//implementation of sayHello() method
	public static void sayHello(){
	   System.out.println("Print val: "+ var);//compiler error
	}

}

OUTPUT

Print val: 1

 

Our code executed successfully and produced desired output.

 

Thus don't try to access non-static field or variable from within a static method to avoid above compiler error.

 


Java Interview Questions
Java Error and Solution

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Sonu Pandit
I am editor in chief at tutorialsinhand.com responsible for managing, reviewing and sending articles/contents for final approval to get published. Connect with me@https://www.linkedin.com/in/sonu-pandit-a77b471ab/. Join write4us program & share your skill
Page Views :    Published Date : Aug 15,2020  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!