Why is a non-static variable not able to be referenced from a static context?

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.

Read more here

Browse More Popular Posts

Leave a Comment