Javatpoint Logo
Javatpoint Logo

Why non-static variable cannot be referenced from a static context in Java

The error non static variable cannot be referenced from a static context in Java is mostly faced by the beginners at the time of compilation of Java program. The reason to occur this error is that they use a non-static member variable in the main() method. Because the main() method in Java is a static method and it is invoked automatically, we need not to create an object to invoke it. To understand the error, first we should understand the static and non-static method in Java.

Static Method

In Java, public methods belong to an instance of class but if we talk about static method, they belong to a class not to an instance of a class. There is no need of creating an instance of the class to invoke a static method. The static member can access only the static data member and can change its value.

Non-static Method

All the methods without having static keyword before method name are referred to as Non-static methods. There is no need to create an instance of the class for accessing the static method and static variable. The non-static methods are used dynamic or runtime binding. Unlike static method, we can override the non-static method.

Let's create a Java program and generate the same error.

In the following example, we have declared a private variable number of type int before the main() method. In the main() method, we are trying to increment the number by 1. It is to be noted that the main() method is a static method and the variable is not static. When we compile the above program, we get the same error, as we have shown below.

StaticExample1.java

Output:

Why non-static variable cannot be referenced from a static context in Java

Now, let's declare the variable number as static and compile the code. Note that, variable and the main() method both are static.

StaticExample2.java

Output:

Why non-static variable cannot be referenced from a static context in Java

The program compiles and run successfully.

Each instance of a non-static variable has a different value and is created when the new() operator initializes an instance of an object. The static variables are created or initialized when the class loads into JVM.

We need an instance of an object for calling the non-static variable. We can create many objects by giving different values to that non-static or instance variable.

StaticExample3.java

Output:

Why non-static variable cannot be referenced from a static context in Java

In the above program, three objects, var1, var2, var3, are created for the class variable and assigned the three different values 12, 13, 14 for the objects var1, var2, and var3, respectively. Now, the number property of each object has its own integer value. When we try to increment the value of the number property by calling the increment() method, the compiler doesn't understand for what value of number the method should increment the value. The compiler faces the ambiguity error and throws the compile-time error non static variable cannot be referenced from a static context.

Solution to The Error

There is one simple way of solving the non-static variable cannot be referenced from a static context error. In the above code, we have to address the non-static variable with the object name. In a simple way, we have to create an object of the class to refer to a non-static variable from a static context. A new copy of all the non-static variables is created when a new instance of variable is created. So, we can access these variables by using the reference of the new instance of the class. for example, consider the following Java program.

StaticExample4.java

Output:

Why non-static variable cannot be referenced from a static context in Java

Description

In the above program, we access the number property by using the class name staticExample. We create an object of the StaticExample class name test and increment the number's value by using the test object.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA