Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

Commonmark migration
Source Link

Found an answer in This Javarevisited Article

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.

Found an answer in This Javarevisited Article

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.

Found an answer in This Javarevisited Article

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.

Source Link
Foggzie
  • 9.9k
  • 1
  • 33
  • 49

Found an answer in This Javarevisited Article

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.

lang-java

AltStyle によって変換されたページ (->オリジナル) /