Error
cannot make a static reference to the non-static field
Error Type
Compile Time
Compile Time
Sample Code
public class Test {
private String element = "Hello";
public static void main(){
element="World";
}
}
Cause
Static method can only access static member elements. Here method is static whereas the accessed variable is non static.
Resolution
Either we should make element as static
public class Test {
private int element;
private static String element = "Hello";
public static void main(){
element="World";
}
}
or
access the variable in some non static method
public class Test {
private String element = "Hello";
public static void main(){
Test test=new Test();
test1.method();
}
private void method1(){
element = "World";
}
}