This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The instance variable in singleton is public

 
T . M Badr
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an example of a singleton implementation in Genesh and Sharma book, the class instance variable that represents an object of that class is declared as public, as I know it needs to be private, but I see no difference except that users can access that variable and set it to null, which can't be done when it's declared as private.

 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it's not a singleton now, is it? Try this:
 
T . M Badr
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see that it's no different than:

 
T . M Badr
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that some code can change the instance directly to null, and other code may access the instance without having to call the getInstance method, which may cause a NullPointerException, the exception can't be fired if the method is the only way to access the instance.
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
T . M Badr wrote:I see that it's no different than:

Yes it is. In Matthew's code, setting Logger.myInstance to null means that the next time you call getInstance a new instance of your class will be created (because the condition on line 8 will evaluate to true again),
but logger1 will still reference the first instance. So you have two instances of your class - which is kind of missing the point of singleton classes.
 
Abhay Agarwal
Ranch Hand
Posts: 1375
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Difficult to say ...

Singleton pattern says that at a time, there can be a single instance of object [ designed to be Singleton]. Pattern do not say that we can not recreate this instance if it has been destroyed.
in this case, object has been destroyed and therefore next request shall fetch new instance again. normally singleton objects are db connection objects, ldap connection or logger instance.
Having said, if we are destroying an singleton object once it has been created, extra care needs to be taken to avoid any unwanted null pointer exception or any kind of exception scenario.
One scenario is already shown in code.



Here, logger1 reference is set to null


 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Agarwal wrote:Difficult to say ...

Singleton pattern says that at a time, there can be a single instance of object [ designed to be Singleton]. Pattern do not say that we can not recreate this instance if it has been destroyed.

But the singleton pattern does say that the class itself is responsible for ensuring there is only one instance of it. If maintaining it's uniqueness is the responsibility of the client code then there's no need for the pattern at all - just choose to create a single instance.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not asking this cause the discussion is on singletons.

You have a public getInstance() method. So why is the instance public?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic