Many Java beginners fail to successfully compile their Java programs just because they try to access a non-static member variable of a class from a static context. To start with let me explain what is a static context and then we shall move on accessing non-static variable from static context issue.

A static context can be either a static method in a class or it could be a static block. The following code shows the both a static method and static block:

public class MyTest{
    static int a=10;
    static{
        Test();
    }
    public static void Test(){
        System.out.println(a);
    }
}

You may notice that we have a static variable, a static method and a static block (from where we invoke the static method). The static method and static block are known as static context. Now let us move to the compiler error about non-static variable being not accessible from static context.

Eclipse reports error

In the above code, if I make the variable a as non-static, we shall get a compiler error in Eclipse IDE which states:

Cannot make a static reference to the non-static field a

In Eclipse IDE, you will also get to know the line number and a red mark on the line where we are trying to access variable a. The reason for this Java compiler error is that static member variables are associated with all instances of a class where as each instance of a class has a copy for non-static member variables. Thus if we access a non-static member (variable a in our case), we don’t know which instance’s copy of variable a are we referring to. This results in error which looks logical.

Common scenario where beginners fail

I have seen some areas where errors are thrown due to incorrect programming. Let us see some cases where the developer thinks he is accessing the variable without any issue but has subtle issues in the code.

a) Invoking non-static methods from static method: Have a look at following code:

public class MyTest {  
  
    void read() {  
        try {  
            ........
            ........
            while (resultSet.next()) {  
                String url = resultSet.getString("url");  
                System.out.println(url);  
            }  
        } catch (Exception e) {  
            throw e;  
        }  
    }  
  
    public static void main(String[] args) {  
        new MyTest();  
        MyTest.read();  
    }  
}

The problem in above code is that the developer is has created an instance of MyTest class in the main method but is still calling the non-static read method by using the class name. The following main method should be used in the above case:

    public static void main(String[] args) {  
        MyTest m = new MyTest();  
        m.read();  
    }  

b) Trying to access a non-static member variable from static method: non-static member variables are similar to non-static methods in terms of individual copy being present for each instance of class. Thus they also need to be accessed using an instance of class.

public class MyTest{
    int a=10;
    static{
        Test();
    }
    public static void Test(){
        System.out.println(a);
    }
}

In order to fix the error in above code, we need to write the following fix:

public class MyTest{
    int a=10;
    static{
        Test();
    }
    public static void Test(){
        MyTest m = new MyTest();
        System.out.println(m.a);
    }
}

The above code will definitely fix the compiler error in Eclipse IDE as “Cannot make a static reference to the non-static field”. Note that the command line compilation using JDK javac tool results in error for this issue as : “non-static variable a cannot be referenced from a static context”. Both of these error refer to the same problem with the code.

Common reasons for non-static variable cannot be referenced from a static context admin Core Java
Many Java beginners fail to successfully compile their Java programs just because they try to access a non-static member variable of a class from a static context. To start with let me explain what is a static context and then we shall move on accessing non-static variable from static...
Many Java beginners fail to successfully compile their Java programs just because they try to access a non-static member variable of a class from a static context. To start with let me explain what is a static context and then we shall move on accessing non-static variable from static context issue. A static context can be either a static method in a class or it could be a static block. <strong>The following code shows the both a static method and static block:</strong> 1 You may notice that we have a static variable, a static method and a static block (from where we invoke the static method). The static method and static block are known as static context. Now let us move to the compiler error about non-static variable being not accessible from static context. <h2>Eclipse reports error</h2> In the above code, if I make the variable a as non-static, we shall get a compiler error in Eclipse IDE which states: <blockquote>Cannot make a static reference to the non-static field a</blockquote> In Eclipse IDE, you will also get to know the line number and a red mark on the line where we are trying to access variable a. The reason for this Java compiler error is that static member variables are associated with all instances of a class where as each instance of a class has a copy for non-static member variables. Thus if we access a non-static member (variable a in our case), we don't know which instance's copy of variable a are we referring to. This results in error which looks logical. <h2>Common scenario where beginners fail</h2> I have seen some areas where errors are thrown due to incorrect programming. Let us see some cases where the developer thinks he is accessing the variable without any issue but has subtle issues in the code. <strong>a) Invoking non-static methods from static method:</strong> Have a look at following code: 1 The problem in above code is that the developer is has created an instance of MyTest class in <a href="http://www.javaexperience.com/java-marker-interfaces/">the main method</a> but is still calling the non-static read method by using the class name. The following main method should be used in the above case: 1 <strong>b) Trying to access a non-static member variable from static method:</strong> non-static member variables are similar to non-static methods in terms of individual copy being present for each instance of class. Thus they also need to be accessed using an instance of class. 1 In order to fix the error in above code, we need to write the following fix: 1 The above code will definitely fix the compiler error in Eclipse IDE as "Cannot make a static reference to the non-static field". Note that the command line compilation using <a href="http://www.javaexperience.com/java-difference-between-jvm-jre-jdk-jit/">JDK</a> javac tool results in error for this issue as : "non-static variable a cannot be referenced from a static context". Both of these error refer to the same problem with the code.
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.