"non-static variable cannot be referenced from a static context" is biggest nemesis of some one who has just
started programming and that too in Java. Since main method in java is most popular method among all beginners and
they try to put program code there they face "non-static variable cannot be referenced from a static context" compiler error when they try to access a non static member variable inside main in Java which is static. if you want to know
public class StaticTest {
private int count=0;
public static void main(String args[]) throws IOException {
count++; //compiler error: non-static variable count cannot be referenced from a static context
}
}
Why non static variable can not be called from static method
Now before finding answer of compiler error "non-static variable cannot be referenced from a static context", let's have a quick revision of static. Static variable in Java belongs to Class and its value remains same for all instance. static variable initialized when class is loaded into JVM on the other hand instance variable has different value for each instances and they get created when instance of an object is created either by using new() operator or using reflection like Class.newInstance(). So if you try to access a non static variable without any instance compiler will complain because those variables are not yet created and they don't have any existence until an instance is created and they are associated with any instance. So in my opinion only reason which make sense to disallow non static or instance variable inside static context is non existence of instance.
In summary since code in static context can be run even without creating any instance of class, it does not make sense asking value for an specific instance which is not yet created.
How to access non static variable inside static method or block
You can still access any non static variable inside any static method or block by creating an instance of class in Java
and using that instance to reference instance variable. This is the only legitimate way to access non static variable
on static context. here is a code example of accessing non static variable inside static context:
public class StaticTest {
private int count=0;
public static void main(String args[]) throws IOException {
StaticTest test = new StaticTest(); //accessing static variable by creating an instance of class
test.count++;
}
}
So next time if you get compiler error “non-static variable cannot be referenced from a static context” access static member by creating an instance of Class. Let me know if you find any other reason on why non-static variable cannot be referenced from a static context.
Other Java Tutorials you may find useful:
Java main() method cannot access a non-static member of its class. That means, public static void main(String[] args) is a static method. Instance variables (variables defined in the class but not marked as static keyword) cannot be accessed from a static method without referencing an instance of the class. An attempt to use the variables and methods of the class which do not have the static modifier without creating an instance of the class is caught by the Java compiler at compile time and flagged as an error: Cannot Be Referenced From a Static Context . To solve your problem, you need to create an instance of your class and then you can access the methods and variables of the class that have not been declared with the static modifier .
BalasHapusStatic methods are useful if you have only one instance where you're going to use the method, and you don't need multiple copies (objects). Non-static methods are used if you're going to use your method to create multiple copies.