aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question regaring Singleton." Watch "Question regaring Singleton." New topic
Author

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

Result:
C:\Java\EigeneJavaProgramme>java Singleton_Test
3
44
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
Sheriff

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 http://www.javageeks.com/Papers/JavaStatics/JavaStatics.pdf


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question regaring Singleton.