• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Question regaring Singleton.

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code below is a Singleton which means that only one instance of class Singleton can be created.
But I was able to create tho instances in class
Singleton_Test but the two instances hat the same
content regarding instance variable number which is 4 in bot instances.
Why is it possible to create two instances from class singleton and why do all singleton instances have the content of the last created Singleton instance?
Appreciate your answers.
Thomas

Result:
C:\Java\EigeneJavaProgramme>java Singleton_Test
3
44
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You did not create two instances, only one.
You have two variables which reference the same singleton instance.
You can test that by checking that instance==instance1.
The reason they both have the same contents is because they are the same object.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...but if you really did want to (kinda) subvert the notion of static in Java, take a look at http://www.javageeks.com/Papers/JavaStatics/JavaStatics.pdf
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic