• 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

confusion with static variable....

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// test results challenged....

class Test
{
static int s;
public static void main(String [] args)
{
Test p = new Test();
p.start();
System.out.println(s);
}

void start()
{
int x = 7;
twice(x);
System.out.print(x + " ");
}
void twice(int x)
{
x = x*2;
s = x;
}
}


In the above mentioned case, the code will compile without error . Where as my understanding is that we cannot access static variable from a nonstatic method, provided we are not instantiating the class in to that method.
Clearing the doubts on this will be of great help.
Thanks in advance.
Bhaumik.
 
author and iconoclast
Posts: 24207
46
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
Sounds like you're memorized a rule, but memorized it incorrectly. Instead, let's try to get you to understand.

Instance variables are part of the objects of a class. If class A has a member x, then if there are 100 A's, then there are 100 x's, one inside each A object. It doesn't make any sense to try to refer to "x" without specifying which instance of "A" we're talking about -- it's like asking for the age without specifying which person's age we're talking about. It has no meaning. We have to say "the x of this A".

Since static methods in class A aren't "attached" to an individual object, the body of a static method can't just say "x" -- it must have an actual instance of the class A so that it can say "the x in this A."

So that's the rule: code in static methods can't refer to instance ("non-static") variables without having an object reference to use; that's pretty much the opposite of what you said here.

I'm going to move this to SCJP, as it's hardly an advanced Java question.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Where as my understanding is that we cannot access static variable from a nonstatic method, provided we are not instantiating the class in to that method.



We can access static variable from non static method without instantiating that class
Example:

int x;
static int y;
public static void main(String[] args) {
// TODO code application logic here
//System.out.println(x);// cant access instance variable, without making object
statc s = new statc();
System.out.println(s.x);
s.nonStatic();
}
public void nonStatic()
{
System.out.println(y);//static variable can be access from nonstatic method
}
 
babai bhaumik
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks a lot, to both of you. It has cleared my doubt.
Thanks again.
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic