Error
Cannot make a static reference to the non-static method
Cannot make a static reference to the non-static method
Error Type
Compile Time
Compile Time
Sample Code
public class Test {
private String getElement(){
return "Hello";
}
public static void main(){
System.out.println(getElement());
}
}
Cause
Static method cannot call instance methods directly. Here calling method is static whereas the called method is non static.
Resolution
Either we should make called method as static
public class Test {
private static String getElement(){
return "Hello";
}
public static void main(){
System.out.println(getElement());
}
}
or
access the method using object reference
public class Test {
private String getElement(){
return "Hello";
}
public static void main(){
Test tst = new Test();
System.out.println(tst.getElement());
}
}