• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

The instance variable in singleton is public

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But it's not a singleton now, is it? Try this:
 
T . M Badr
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see that it's no different than:

 
T . M Badr
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic