| Author |
Uniqueness of static variable
|
Balakrishna Thati
Greenhorn
Joined: Mar 12, 2008
Posts: 23
|
|
Pragramatically how to show that there is only one instance of Static variable irrespective of number of objects...
I thought of doing it by
1. showing their values being equal.. But the counter argument is that the "having the same value doesn't sufficient to say they are from the same variable"
2. equating the static variable's hash code accessing it from different objects. But Hashcode can't guarantee uniqueness.
is there any other way to prove that irrespective of number of objects there is only one static variable exists for a Class.
-- KVT
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
You mean apart from reading the Java™ Language Specification?
Create an Object instance which is static, and access this from each instance of your class, and use its equals() method which reads something like this:
|
 |
Balakrishna Thati
Greenhorn
Joined: Mar 12, 2008
Posts: 23
|
|
Thanks Ritchie,
Here is my code
|
 |
Jonas Isberg
Ranch Hand
Joined: Mar 18, 2003
Posts: 118
|
|
Balakrishna Thati wrote:System.out.println("We have only one instance");
Yes and no I would say. The only instance you have is of the type Abc. count is a primitive
type that is compared by value (and gets a default value of 0).
What if the code had been like below, would that prove anything?
|
 |
Balakrishna Thati
Greenhorn
Joined: Mar 12, 2008
Posts: 23
|
|
if the comparision is between the primitives, it again fall back to my option 1, that i stated earlier.
To conclude it finally, i changed the code little bit.
|
 |
Jonas Isberg
Ranch Hand
Joined: Mar 18, 2003
Posts: 118
|
|
class [surl='http://www.javaranch.com/unit-testing.jsp' class='api' title='article: evil unit testing wrote:Test[/surl]]true
So, have we proven anything?
If I wanted to prove to myself that static actually works, I would change the static member
and see if it is really changed when accessing from another instance.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
|
I don't think you are doing anything useful with that count. You can prove two objects are the same object with the == operator. On the object references. Since you have no way of increasing the count variable, it will always be th same for all instances of that class. If you did increment it, then all instances would have the same increment and th same value. So your test is no use to you.
|
 |
Balakrishna Thati
Greenhorn
Joined: Mar 12, 2008
Posts: 23
|
|
How about this ?
|
 |
Jonas Isberg
Ranch Hand
Joined: Mar 18, 2003
Posts: 118
|
|
If that code gives different outputs when changing from static to not static,
then I would say you have proven something.
|
 |
Balakrishna Thati
Greenhorn
Joined: Mar 12, 2008
Posts: 23
|
|
Is this ok ? or any thing else missed ??
|
 |
Jonas Isberg
Ranch Hand
Joined: Mar 18, 2003
Posts: 118
|
|
Now you both check so everything actually references the same instance
and you use it (for counting the instances) in way that would have failed
if it was not static. Well done!
(Don't forget that indentation helps readability. )
|
 |
Balakrishna Thati
Greenhorn
Joined: Mar 12, 2008
Posts: 23
|
|
|
Thanks Isberg and Ritchie... we done it flawlessly atlast
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
You're welcome
|
 |
 |
|
|
subject: Uniqueness of static variable
|
|
|