Can static method access non-static variables from subclass/any other class? Can non-static method access static variables/final static variables from subclass/any other class?
Please expalin with example.
Thanks<br />Dinesh
Ameen khan
Ranch Hand
Joined: Jun 10, 2007
Posts: 52
posted
Can static method access non-static variables from subclass/any other class? Can non-static method access static variables/final static variables from subclass/any other class?
Static methods can't access non-static variable because you can acccess static method through class name so how do you know which objects non-static instance variable you are accessing
yes non tatic method can access static variables
SCJP 5.0<br />Next-> I Don't Know
Mani Ram
Ranch Hand
Joined: Mar 11, 2002
Posts: 1140
posted
Originally posted by dinesh tahiliani:
Please expalin with example.
Why not just try yourself with an example an see what happens? That way you will remember the details for a longer time (if not forever). After you have tried, if you have any issue with understanding the way it works, come back and ask.
Mani
Quaerendo Invenietis
Raef Kandeel
Ranch Hand
Joined: Aug 05, 2007
Posts: 87
posted
Hello There,
Can static method access non-static variables from subclass/any other class?
Yes they may:-
Can non-static method access static variables/final static variables from subclass/any other class?
static void staticMethod() { System.out.println("In staticMethod(): " + s1); //System.out.println(s2); //ERROR, due to static }
public static void main(String[] args) { System.out.println("In main(): " + s1); //System.out.println(s2); //ERROR, Can't make static reference to non-static field //nonStaticMethod(); //ERROR, Can't make static reference to non-static method staticMethod(); } }
Lucy Smith
Greenhorn
Joined: Aug 07, 2007
Posts: 17
posted
But from what Raef said, you could reference the static variable by using class.static variable, right? So the above example can be modified as follows:
static void staticMethod() { System.out.println("In staticMethod(): " + s1); System.out.println(new StaticTest().s2); //can use now with class.staticVariable }
public static void main(String[] args) { System.out.println("In main(): " + s1); System.out.println(new StaticTest().s2); //can use now with class.staticVariable new StaticTest().nonStaticMethod(); //can use now with class.staticMethod() staticMethod(); } }
So would that be correct to say that you cannot use non-static variable BY ITSELF in a static method?
static void staticMethod() { System.out.println("In staticMethod(): " + s1); System.out.println(new StaticTest().s2); //can use now with class.staticVariable }
In the above code, you have s2 as normal variable(non-static variable), then how can you access this with class name.Non static variables are accessed thru object reference. Can you little elaborate on this plss
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
Originally posted by dinesh tahiliani: In the above code, you have s2 as normal variable(non-static variable), then how can you access this with class name.
Since s2 is not accessed by class name. Access by class name means StaticTest.s2, and this is only available for static members. Instead, Lucy has created a new instance of the class, and then s2 is accessible through that instance.
Dinesh Tahiliani
Ranch Hand
Joined: Aug 06, 2007
Posts: 486
posted
Thanks
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3057
posted
Originally posted by Lucky Lucy:
So would that be correct to say that you cannot use non-static variable BY ITSELF in a static method?
Yes. Thats correct. If at all you have to use, you have to have an instance as the instances can access the static variables.
Welcome to JavaRanch, Lucky Lucy! [ August 10, 2007: Message edited by: Raghavan Muthu ]
Originally posted by Lucky Lucy: So would that be correct to say that you cannot use non-static variable BY ITSELF in a static method?
Yes you could say so. Because a non-static variable never stands for itself, it belongs to an object. Contrary to this a static variable belongs to the class. If you want to access a non-static method of the same class, you will need an object of the class. That's what you did in your line new StaticTest().nonStaticMethod(); //can use now with class.staticMethod()
non-static fields (variables and methods) belong to the object, and every object has its own one. static fields belong to the class, and it doesn't matter how many objects there are, when a variable is static, there is only one for the class.
All objects share this static variables. This can be a trap. Normally you access static variables through its class name (or without any name when it is in the same class).
But it is also allowed (but should be avoided) to access them via an objects reference.
Example:
The output is:Harry's legs: 6 Frank's legs: 6 Incremented Harry's legs. Harry's legs: 7 What happened to Frank? Frank's legs: 7
If you make the variable non-static, everything would be all right. Because the legs variable should belong to the object and not to the class.
Now something els: Dear "Lucky Lucy" !
Thanks for your first contribution to this forum and...
Welcome to the Ranch!
Hope you'll enjoy.
Only one small issue: The Java Ranch follows a certain policy regarding user names. The main reasons why and a link how to change yours you'll find here: http://www.javaranch.com/name.jsp
So, could you please change your user name before your next posting? It will not affect anything you've already posted here. Just your user name will update.
I'm posting this because I am one of the moderators of this forum.
Yours, Bu.
all events occur in real time
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3057
posted
Originally posted by Burkhard Hassel:
Now something els: Dear "Lucky Lucy" !
Thanks for your first contribution to this forum and...
Welcome to the Ranch!
Hope you'll enjoy.
Only one small issue: The Java Ranch follows a certain policy regarding user names. The main reasons why and a link how to change yours you'll find here: http://www.javaranch.com/name.jsp
So, could you please change your user name before your next posting? It will not affect anything you've already posted here. Just your user name will update.
I'm posting this because I am one of the moderators of this forum.
Yours, Bu.
That was a nice example Bu. Is the name "Lucky Lucy" look like a ficticious one? I dint get that that why i dint suggest her to do so!
Raef Kandeel
Ranch Hand
Joined: Aug 05, 2007
Posts: 87
posted
let's talk Java
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3057
posted
Originally posted by Raef Kandeel: let's talk Java