• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why non static context cannot be referenced from a static context

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 61
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Anupam Jain
Ranch Hand
Posts: 61
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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....

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Marshal
Posts: 79466
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anupam Jain wrote: . . .. Please correct the spelling... Its not "Anupama" but "Anupam".

Done
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot friends. Now I understood clearly.
 
reply
    Bookmark Topic Watch Topic
  • New Topic