Java is one of the most popular and widely used programming language and platform. Java is Object Oriented. However, it is not considered as a pure object-oriented as it provides support for primitive data types (like int, char, etc). In java, methods can be declared as either static or non-static. In this article, let’s discuss why non-static variable cannot be referenced from a static method.
Before getting into the error, lets first understand what each of the methods means:
- Static Method: A static method is a method that belongs to a class, but it does not belong to an instance of that class and this method can be called without the instance or object of that class. In the static method, the method can only access only static data members and static methods of another class or same class but cannot access non-static methods and variables.
- Non-static method: Any method whose definition doesn’t contain the static keyword is a non-static method. In the non-static method, the method can access static data members and static methods as well as non-static members and method of another class or same class, also can change the values of any static data member.
What is the Issue with non-static variable referenced from static context?
Let’s consider the following code where class A is created with a non-static variable and a static method. The object of this class is being created in another class and the static method is being accessed as follows:
IF this code could actually be run, you would expect the output to be:
4 5 6
But instead a compile-time error is thrown
Compile Errors: prog.java:16: error: non-static variable N cannot be referenced from a static context N++; ^ 1 error
As we can see that the above program gives the error. Though in the above code, all the objects names have the same variable name N, if we try to increment N, its giving an error. This error is very common during object oriented programming.
Why does this error occur?
For the non-static variable, there is a need for an object instance to call the variables. We can also create multiple objects by assigning different values for that non-static variable. So, different objects may have different values for the same variable. In the above program we have created three objects obj1, obj2, obj3 for the class A and assigned the three different values 3, 4, 5 for the objects obj1, obj2, obj3 respectively. When we try to call the function increment, as every object of N have its own value there will be ambiguity for the compiler to understand for what value of N should the method increment the value.
How to solve this error?
In order to avoid ambiguity, the java compiler throws a compile time error. Therefore, this issue can be solved by addressing the variables with the object names. In short, we always need to create an object in order to refer to a non-static variable from a static context. Whenever a new instance is created, a new copy of all the non-static variables and methods are created. By using the reference of the new instance, these variables can be accessed. For example:
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Internal static variable vs. External static variable with Examples in C
- Why Java Interfaces Cannot Have Constructor But Abstract Classes Can Have?
- Difference between static and non-static method in Java
- Static and non static blank final variables in Java
- Difference between static and non-static variables in Java
- Understanding storage of static methods and static variables in Java
- Understanding "static" in "public static void main" in Java
- Final static variable in Java
- Why can't static methods be abstract in Java?
- Why a Constructor can not be final, static or abstract in Java?
- Static Block and main() method in Java
- Static method in Interface in Java
- Shadowing of static functions in Java
- Are static local variables allowed in Java?
- Assigning values to static final variables in Java
- Comparison of static keyword in C++ and Java
- Static blocks in Java
- Static class in Java
- Static vs Dynamic Binding in Java
- Static methods vs Instance methods in Java
Improved By : anjuitsme