aspose file tools*
The moose likes Beginning Java and the fly likes static variables - belong to the class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static variables - belong to the class." Watch "static variables - belong to the class." New topic
Author

static variables - belong to the class.

rama murthy
Ranch Hand

Joined: Jan 13, 2006
Posts: 82
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: 2367
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

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: 2367
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
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
it's allowed simply because a class instance knows its type.


42
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

Originally posted by rama murthy:

Why it is allowed.

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...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
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
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
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


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
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
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
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
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
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.

Here is some fun:

Taken from http://jqa.tmorris.net/GetQAndA.action?qids=39


Tony Morris
Java Q&A (FAQ, Trivia)
Raghav K Aggarwala
Greenhorn

Joined: Jun 04, 2006
Posts: 9
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
-----------------------------------------


Every time you run a program separately, the original value of static variable t is loaded (in your case 6).

Once you have changed this value using any instance, it will be changed for the rest of the run of that program.

Static variables belong to the class for that specific run of the program.

I hope this will help.

Raghav
rama murthy
Ranch Hand

Joined: Jan 13, 2006
Posts: 82
Thanks Raghav Aggarwal.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static variables - belong to the class.