File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Static nad non static Access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static nad non static Access" Watch "Static nad non static Access" New topic
Author

Static nad non static Access

Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167


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


Varuna Seneviratna
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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 ?


[My Blog]
All roads lead to JavaRanch
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 ]

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
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

Joined: Aug 05, 2008
Posts: 378
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

Joined: Oct 13, 2005
Posts: 40034
    
  28
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
 
subject: Static nad non static Access