Which is the Best Java Web Framework ?

Java - Can we access instance variables within static methods ?

Ans. No and Yes.

No, because we cannot access them directly and Yes, because we can access them using object reference.


Static methods belong to a class and not objects whereas non static members are tied to an instance. Accessing instance variables without the instance handler would mean an ambiguity regarding which instance the method is referring to and hence its prohibited.

See this picture and imagine the Class as a mold and flowers being 2 objects. 

Static methods belong to the mold and the member elements belong to the flower object. 

Lets say you want to access color ( instance variable ) within static method. Without object specifier or handler, its ambiguous for the compiler to tell which object you are referring to. Though you can always access using flower1.color or flower2.color where flower1 and flower2 are object references.    
Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
comments powered by Disqus


Try our new Java Interview Questions Search Tool.

There are more than 1000 questions with practice tests.

It's updated daily and It's Awesome.


Try our new Java Practice Test tool.

There are more than 200 questions and expanding quickly. It's updated daily and It's Awesome.