If static variables belong to the class and NOT part of the object then how come we are able to invoke it with an object reference variable and also modify its value.
class AQ { static int t = 6; }
class Tr { public static void main(String[] args) { AQ q = new AQ(); System.out.println(q.t); q.t = 5; System.out.println(AQ.t); new Tr().call(); }
void call() { System.out.println(new AQ().t); } }
Output:
6 5 5
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
posted
0
That is allowed in Java. But be careful.
If you use an object reference to call a static method in a class definition, then it does not matter which object reference you use, even if that reference is null.
Only the type of the reference is considered.
rama murthy
Ranch Hand
Joined: Jan 13, 2006
Posts: 82
posted
0
That is allowed in Java. But be careful.
Why it is allowed.
Then what's the difference between class variables and instance variables.
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
posted
0
The difference is that no matter what instance of an object you use, you can change the class variables. It doesn't have anything to do with an instance.
However, you can only access an instance variable with a particular instance.
rama murthy
Ranch Hand
Joined: Jan 13, 2006
Posts: 82
posted
0
Ya you are correct.
I modified the program a little and your explanation helped me to understand the concept.
Output:
6 10 5 123 5 10
Thanks Keith Lynn.
Jeroen T Wenting
Ranch Hand
Joined: Apr 21, 2006
Posts: 1847
posted
0
it's allowed simply because a class instance knows its type.
42
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
posted
0
Some IDEs (like Eclipse) will mark this with a warning. I always make static references to the class and clear the warning. It does get a little odd sometimes. For example, I'd probably like to use the same reference twice here instead of the object once and the class once.
myObject.someMethod( MyClass.CONSTANT );
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Then what's the difference between class variables and instance variables.
Because the people who invented Java unfortunately decided to allow it. But accessing static member variables through an instance is never necessary and is to be avoided, because it is confusing.
It should not have been possible, but unfortunately it is. I regard this as a mistake in the Java language. The Java language is not perfect you know, it has been invented by human beings...
I don't think it's a mistake. It makes it possible to access the static member by name from inside an instance of the enclosing class without explicitly mentioning the classname. That's a good thing IMO, the capability to access the static member outside an instance of the class itself using an instance name is a mere side effect of it and a logical extension.
Puneet Pathak
Greenhorn
Joined: Jun 09, 2006
Posts: 2
posted
0
We can access variable declared as static through object refference, but the changes made to the static variable will not be applicable only to the object through which we are accessing the variable but to the other class instances as well. where as in case of non-static variable the changes would be instance specific.
Orem Hun
Greenhorn
Joined: May 24, 2005
Posts: 28
posted
0
accessing static member variables through an instance is never necessary and is to be avoided, because it is confusing.
Agree, code is better readable when class variables are referred with class name to differentiate them from instance variables.
rama murthy
Ranch Hand
Joined: Jan 13, 2006
Posts: 82
posted
0
My question is in the call() method of Tr class output for static variable of class AQ is 5 whereas in Ts class output for the same static variable of class AQ is 6.
Why?
Manhar Puri
Ranch Hand
Joined: Aug 23, 2005
Posts: 41
posted
0
I believe you are running the Tr java program first and the running the Ts program.
Try doing this instead of creating a main method in Ts create some xyz method and then in the main method of Tr class create instance of Ts and call the method xyz.
That should help answer your question.
-Manhar.
rama murthy
Ranch Hand
Joined: Jan 13, 2006
Posts: 82
posted
0
Hello Manhar Puri
I just did as you said and got results like
But my question is why am I NOT getting the same result(5) when I try to execute Ts class seperately.
class Ts {
public static void main(String[] args) { AQ q = new AQ(); System.out.println(q.t); }
}
Here output is 6
Ramasubbhu Allur Kuppusamy
Ranch Hand
Joined: Sep 16, 2005
Posts: 120
posted
0
Static Code is accessible through Non-Static Code. But the vice-versa is not possible, mind you.
Regards,<br />Ram.<br />SCJP 1.4
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
posted
0
Originally posted by Ramasubbhu Allur Kuppusamy: Static Code is accessible through Non-Static Code. But the vice-versa is not possible, mind you.
Yes it is.
I know that's not what you had in mind, but it is still accessing non-static code from a static context. The significant difference is that with static methods the reference to the object it is invoked on is implicit.
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
Originally posted by Jeroen T Wenting: I don't think it's a mistake. It makes it possible to access the static member by name from inside an instance of the enclosing class without explicitly mentioning the classname. That's a good thing IMO, the capability to access the static member outside an instance of the class itself using an instance name is a mere side effect of it and a logical extension.
It has been publicly admitted as a mistake, and it is poor practice to reference a static member with an implicit 'this' reference.