This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static nad non static Access

 
Varuna Seneviratna
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above program works and the size variable which is declared in the Dog class in a non static context is accessed and it works But The below program generates an error as "non-static variable x cannot be referenced from a static context pvb.go(x);".The above program accesses an non static variable from a static context and the only difference is the access statement is in a another class.But when the non static variable and the static method which accesses it is in the same class it is not possible to access the non static or instance variable.What is the theory behind the accessing static and non static variables from static and non static contexts




Regards Varuna
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the size variable which is declared in the Dog class in a non static context is accessed and it works

You are accessing the size variable of the one/two/three dog instances. A dog instance exists, therefore you can access its varable.

The above program accesses an non static variable from a static context and the only difference is the access statement is in a another class.

The biggest difference is that x is an instance member of PassByValue. In pvb.go(x); you are referrencing a non static variable from a static context (the main method). Which instance of x are you trying to access ? You don't know, and the compiler doesn't know either.

What would happen if you had pvb.go(pvb.x); instead ?
 
Sagar Rohankar
Ranch Hand
Posts: 2905
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Varuna Seneviratna:


The above program works and the size variable which is declared in the Dog class in a non static context is accessed and it works


It works because you are creating an object of class Dog in static method main(), which in turn access non static 'bark()' method , which is perfectly legal !!

Originally posted by Varuna Seneviratna:

But The below program generates an error as "non-static variable x cannot be referenced from a static context pvb.go(x);".The above program accesses an non static variable from a static context and the only difference is the access statement is in a another class.But when the non static variable and the static method which accesses it is in the same class it is not possible to access the non static or instance variable.What is the theory behind the accessing static and non static variables from static and non static contexts






Here 'x' is an instance variable , and you can not access it from static main() method !

[ September 30, 2008: Message edited by: Sagar Rohankar ]
[edit]Add newlines and/or + to very long lines. CR[/edit]
[ September 30, 2008: Message edited by: Campbell Ritchie ]
 
Varuna Seneviratna
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christophe
You exactly provided me with what I wanted to here.My point was if the instance variable was accessed even from an static context,and the object reference variable supplied, the JVM should be able to find it.But now I understand that for that you have to specify object name plus the instance variable within parenthesis to point exactly to the particular instance variable and also that from a static context if only the name of the instance variable is mentioned the JVM looses it's track, to avoid that specify the object variable also

am I right?


Thanks Varuna
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All instance methods gets an implicit reference "this" passed to it. Static methods do not.

The instance method would use "this.x" if you put that piece of code in an instance method instead of a static method.

Suppose you used another static method instead of main, as you said the relevant object reference could be passed as say, obj and accessed as obj.x
 
Campbell Ritchie
Sheriff
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct about "this" being passed; in fact if you go through the BCEL Manual or similar it says there is an additional parameter added to all methods to take the "this."

You are not allowed to use "this" or "super" in a static method.

If "x" is a field, it ought to be given private access so you could only say something.x inside the same class; everywhere else you should use a method to gain access to "x."
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic