wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Why non static context cannot be referenced from a static context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why non static context cannot be referenced from a static context" Watch "Why non static context cannot be referenced from a static context" New topic
Author

Why non static context cannot be referenced from a static context

krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
Hi everybody I am Krishna, as a beginner I have few doubts, when I was coding, I referred a non static variable from static function, then I got a compile time error saying cannot refer to a non static context from static context. Can anybody tell me why I cannot do so.

I made a search before asking this question, but I did not understand that, please explain me in detail. And when I was searching they said that non static context can be referred from main() method if I create an object to the class. This is not applicable to other static methods. Why?

Thank you all in advance. Have a nice day.


There is no rule that we all should know everything. Lets learn few things of everything here.
Anupam Jain
Ranch Hand

Joined: Mar 16, 2010
Posts: 61

Hello Krishna,
The answer to your question is a self analysis of the situation.

The rule is that we don't need an object of the class to access the static members of that class, while ...
for non-static members we need to have the object...

Now consider the situation at hand... when you are referring to something non-static from static context.... isn't it same as referring to it without having any object???
yeah that's true... it certainly is...

so that solves the problem why you can't refer to something non-static from a static context...

I hope this should be enough for now... post a reply for something you didn't understand...


SCJP-6.0 OCPJWCD-5.0
Suhas Bilaye
Ranch Hand

Joined: Sep 10, 2009
Posts: 80
Hi Krishna,
Welcome to Java Ranch!!!

In extension to what Anupam said.. ...

What is static? Static simply means one copy per class. ie. Static variable is part of a class and not of an object of that class. N number of objects of the class refer to the same variable when it is declared as static.

A non static variable is a part of the object of that class. And to access the variable you need to create an object and that object will have its own copy. When you create another object it will have another copy.

So when you try to access static variable from a non static context you will get errors?


Thanks and Regards,
Suhas
http://www.xplore-java.blogspot.com/
Anupam Jain
Ranch Hand

Joined: Mar 16, 2010
Posts: 61

Suhas Bilaye wrote:Hi Krishna,
Welcome to Java Ranch!!!

In extension to what Anupama said.. ...

What is static? Static simply means one copy per class. ie. Static variable is part of a class and not of an object of that class. N number of objects of the class refer to the same variable when it is declared as static.

A non static variable is a part of the object of that class. And to access the variable you need to create an object and that object will have its own copy. When you create another object it will have another copy.

So when you try to access static variable from a non static context you will get errors?




Hey Suhas,

Please correct the spelling... Its not "Anupama" but "Anupam". The former one is a female name....

Oh and by the way... nice extension to explanation....

Vidyakar Sharma
Greenhorn

Joined: Dec 27, 2009
Posts: 17
Hi Anupam,
Go through the whole life-cycle of a typical java program. Compiler does not do something except creating a bytecode for a particular source that you have given to that. While you call your interpreter to execute the program it happens in several phases.
The very first thing that your interpreter does is "Class Loading". And at this phase all the static variables and methods are loaded into the memory. At this stage there is no instance created in the heap. So instance variables can not be used from an static context . Am I right Guys???


Vidyakar Sharma.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Anupam Jain wrote: . . .. Please correct the spelling... Its not "Anupama" but "Anupam".
Done
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
That's correct Vidyakar. Since you don't have instance variables available when static methods are loaded. So you can't use them. But reverse can happen(i.e referring static context from non static context) and reason is very clear.


SCJP 1.5(97%) My Blog
krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
Thanks a lot friends. Now I understood clearly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why non static context cannot be referenced from a static context