Cannot make a static reference to the non-static method or field error in java
In this java tutorial, we will learn about:
-
cannot make a static reference to the non-static method error in java,
-
how to resolve cannot make a static reference to the non static method in java?
-
cannot make a static reference to the non-static field,
-
how to fix cannot make a static reference to the non-static field?
'Cannot make a static reference to the non-static method' - is a compile time error thrown in java.
cannot make a static reference to the non-static method
We get above compiler error in two cases:
-
When we try to call non-static method from within a static method.
-
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. You can also refer video tutorial explaining static reference to non static method java below:
Case I: Calling non-static method from within static method
Whenever we try to make static reference to non static method java, we get the error - cannot make a static reference to the non-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 static reference to non static method java is made. Lets see how:
-
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.
Thus we can conclude that we cannot make a static reference to the non-static method java.
how to resolve cannot make a static reference to the non static method in java?
What we can do?
-
We can change the called method (sayHello) to static as suggested above.
-
If we are not allowed to change method to static, then try to access by creating object of the class using . (dot) operator.
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 make static reference to non static method java 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.
how to fix cannot make a static reference to the non-static field?
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 directly to avoid above compiler error.
You can create object of the class whose non static variable you want to access as shown in below video.
This wraps up our session on cannot make a static reference to the non-static method java & how to resolve cannot make a static reference to the non static method in java, cannot make a static reference to the non-static field & how to fix cannot make a static reference to the non-static field.
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 :
Jul 03,2022