aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes The instance variable in singleton is public Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "The instance variable in singleton is public" Watch "The instance variable in singleton is public" New topic
Author

The instance variable in singleton is public

T . M Badr
Greenhorn

Joined: Mar 05, 2014
Posts: 14
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

Joined: Apr 06, 2010
Posts: 4462
    
    8

But it's not a singleton now, is it? Try this:
T . M Badr
Greenhorn

Joined: Mar 05, 2014
Posts: 14
I see that it's no different than:

T . M Badr
Greenhorn

Joined: Mar 05, 2014
Posts: 14
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

Joined: Aug 05, 2005
Posts: 3739
    
  16
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.


Joanne
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1212
    
    1

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



Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I, CIW Certified Ecommerce specialist
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4462
    
    8

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

Joined: Dec 25, 2013
Posts: 261
    
    4
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
 
subject: The instance variable in singleton is public