The error “non-static variable cannot be referenced from a static context” occurs because non-static variables need an object to use them, while static methods belong to the class and do not have instance access. To fix this, create an object of the class or declare the variable as static.
In this blog, we will discuss the error “non-static variable cannot be referenced from a static context” and how to solve it.
Table of Contents:
Understanding “Non-Static Variable Cannot Be Referenced from a Static Context” Error
In Java, a non-static variable or method can only be accessed from an instance of the class. Whereas a static variable or method can be accessed directly from the class without having the need for an instance.
But if you try to access a non-static variable or method from a static context, such as a static method or the main method, which is always static, you will get a compilation error: “non-static variable cannot be referenced from a static context.
What is a static keyword in Java?
The static keyword in a class means the member belongs to the class itself, not instances of the class. It can be accessed without creating an object of the class. The static keyword belongs to the class rather than an instance of the class.
The static keyword can be:
- Variable (also known as a class variable)
- Method (also known as a class method)
- Block
- Nested class
Master Java Today - Accelerate Your Future
Enroll Now and Transform Your Future
Static methods
A static method is a method that belongs to a class, but it does not belong to an instance of that class. This method can be called without the object of that class. This method can only access static variables and methods of the same class or other classes.
In Java, all non-static methods (regardless of public or private) belong to instances, whereas static methods belong to the class itself. Since they don’t require an object to use them, they are often used for utility functions, generic operations, or mathematical calculations.
Static methods are commonly used to access static variables, but they cannot access instance variables or methods since those are linked to a specific object. Due to their instance-independent nature, static methods and variables are used in stateless utility classes.
Non-static methods
Any method whose definition doesn’t contain the static keyword is a non-static method. These methods can access the static data members and static methods, as well as the non-static members and methods of another class or the same class.
Non-static methods are invoked at runtime, and we can override them.
When an operation is performed and it needs access to an object’s state, non-static methods allow you to work with instance-specific data and behavior. Since non-static methods use the functionality linked to specific instances, they are important when defining an object in object-oriented programming.
What Does the Error Mean?
The error “non-static variable cannot be referenced from a static context” in Java means that a static method or static block is trying to use a non-static variable or method. This is not allowed in Java because static members belong to the class itself, while non-static members belong to their instances of the class.
When a static method tries to use a non-static variable or method without first creating an object of it, Java throws a compilation error to show the rule that non-static members need an instance to use them.
Why does this error occur?
The error “non-static variable cannot be referenced from a static context” in Java is common among beginners, and it occurs at compile time. This happens when a non-static member variable is accessed inside a static method, such as main().
Since static methods belong to a class and are executed without creating an object, they do not have access to non-static variables or methods. Non-static members are linked with an instance of the class, meaning they do not exist in memory until an object is created.
If a static method tries to access a non-static member without creating an object of it, Java will throw a compilation error because non-static members only exist when their object is created.
Example:
Output:
Explanation:
In the above code,
- When you try to use instanceVar inside the staticMethod, it will result in the compilation error: “non-static variable cannot be referenced from a static context” because instanceVar belongs to an instance of MyClass, and static methods do not have access to instance variables.
- instanceVar is a non-static variable, i.e., it is linked to an object of MyClass.
- staticMethod is a static method, which belongs to the class and cannot use any instance variables unless its object is created.
Unlock Your Future in Java
Start Your Java Journey for Free Today
How to Solve this Error?
In Java, the error “non-static variable cannot be referenced from a static context” occurs when a static method tries to access a non-static (instance) variable or method directly without creating an object. To solve this problem, we can use the following solutions.
Solution 1: Creating an Instance of the Class and Calling the Instance Method
This is the most common solution, i.e., creating an object of the class in the static method and then using the non-static variable or method through that object.
This solution is used when you cannot convert the method to static and need to work with object-specific data.
Example:
Output:
Explanation: In the above example, the staticMethod is static, but we are using the non-static variable instanceVar by creating an object obj of the class MyClass and using it to access the non-static variable.
Solution 2: Converting the Instance Method to a Static Method
If the method does not depend on object-specific data, we can convert the instance method to a static method. And it can be called directly without creating an object of it.
When to use this method?
- If the method does not need to use instance-specific data, it can be made static.
- If a method performs a specific task and does not depend on the object’s state, you can convert it to a static method.
Example:
Output:
Explanation: In the above code, staticVar is a static variable whose value is set to 10. The static method prints the value of staticVar. In the main method, staticMethod is called, which prints 10. Since both the variable and the method are static, they can be used without creating an object of the class.
Solution 3: Refactor the Static Method by Creating an Instance of the Class
In some situations, it’s not appropriate to convert the instance method to static, for example, if the method depends on the state of the object. In such cases, the static method can be used to create an instance and then call the instance method on the newly created object.
Example:
Output:
Explanation: In the above code, we are creating an object obj of MyClass inside the static method. Then we call the method instanceMethod on the created object, which uses the non-static variable instanceVar.
Storage Area of Static Variable in Java
In Java, static variables are stored in the Method Area (or Metaspace in Java 8+), which is generally a part of the JVM memory responsible for class-level data such as static fields, methods, and class metadata.
Static variables are shared among all objects, so they are not stored inside each object. Instead, they are stored in a common area, so that all the objects can access the same variable when required. They do not belong to any single object, and they are not stored inside individual objects on the heap.
Real-Life Example of a Static Variable in Java
There are many real-world examples of the Static variable in Java. Some of them are discussed below,
1. Application Configuration
In many applications, there are some values that stay constant throughout the program, such as the maximum number of connections to a database. These types of values are stored as static variables as they can be shared across many instances of the application.
2. Counting Instances of a Class
In many applications, you have to keep track of how many objects are created for a class. This is done by using static variables.
3. Singleton Pattern
In the Singleton Pattern in Java, a static variable is used to ensure that only one object of the class is created.
4. Global Counter
A Global Counter is a variable that is used across the entire program, and it keeps track of a value, usually something that increases or decreases. Static variables are used to create a global counter.
Get 100% Hike!
Master Most in Demand Skills Now!
Conclusion
The error “non-static variable cannot be referenced from a static context” occurs when a static method tries to access a non-static variable without creating an object. To solve this, you can either create an object to access the non-static members, convert the method to a static method if it doesn’t require an object, or create an object inside the static method.
To know more about this topic, you can refer to our Java Course.
Non-static variable cannot be referenced from a static context Error in Java- FAQs
Q1. Can you access a non-static variable in the static context?
No, as static methods are associated with the class itself, not with any specific instance of that class.
Q2. How to access a non-static method from a static method?
To call a non-static variable from a static method, an instance of the class has to be created first.
Q3. What is the difference between a static variable and a non-static variable?
Static variables are shared among all instances of a class. Non-static variables are specific to that instance of a class.
Q4. Is it possible to override non-static methods as static?
No, we cannot override non-static methods as static methods in Java.
Q5. Are static variables global?
A static variable can be either a global or a local variable.
Q6. Are static variables always public?
Static methods can be public or private.