• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Why a Static method cannot refer to an instance variable?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If somebody can explain why a static method cannot refer to an instance varible? if possible please explain with an example...
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A static method can refer to an instance variable, but it must refer to an instance variable of a specific object. You don't need to use an object to call a static method, right? So the static method has no object to refer to, the way that non-static methods refer to "this". But if the static method creates an instance of the class, or otherwise gets ahold of one, it can access all the members of the class, including the private ones, through that instance; i.e.,

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's some example code that won't compile:



The compiler gives the following error:

non-static variable x cannot be referenced from a static context.

or something to that effect. The reason that it won't compile is that the compiler realizes that you have to have an instance of the Example class in order to have an x variable. You don't necessarily need an instance of the class in order to call a static method. The compiler prevents you from accessing the instance variable from a context where there is no guarantee that an object has been instantiated.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a little different spin on what has already been said:

One of my "learning experiences" was about this very topic. What I was doing was, inside of my public class but before the main() method, I was establishing variables. Seemed like the thing to do. Then, the code inside my main() referenced those variables. Seemed like the thing to do again.

What I had to learn was....... main() is declared static. A static method can be called without instantiating the class. NOW...the variables that I had declared were very definitely instance variables of my public class.

Since main() is static and can be called without instantiating the class.... there was no object of the public class created. Therefore, there is no instance variable to refer to.

That is why in many sources I have seen, the public class is instantiated right up at the top. NOW the variable/names exist.
 
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic