Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Uniqueness of static variable

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79637
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ritchie,

Here is my code
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

class Test wrote: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
Marshal
Posts: 79637
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about this ?

 
Jonas Isberg
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that code gives different outputs when changing from static to not static,
then I would say you have proven something.
 
Balakrishna Thati
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this ok ? or any thing else missed ??

 
Jonas Isberg
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Isberg and Ritchie... we done it flawlessly atlast
 
Campbell Ritchie
Marshal
Posts: 79637
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
reply
    Bookmark Topic Watch Topic
  • New Topic