File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Question regaring Singleton. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question regaring Singleton." Watch "Question regaring Singleton." New topic

Question regaring Singleton.

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
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.

C:\Java\EigeneJavaProgramme>java Singleton_Test
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
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.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
...but if you really did want to (kinda) subvert the notion of static in Java, take a look at

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Question regaring Singleton.
It's not a secret anymore!