• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

protected constructor in Singleton class

 
Ranch Hand
Posts: 99
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends ... I have a doubt related to access modifier of constructor of singleton class

class Singleton{
public static Singleton Instance() {
if (_instance == null)
_instance = new Singleton();
return _instance;
}
protected Singleton() {}
private static Singleton _instance = null;
}

I know that if we use protected constructor other classes can create Singleton instance using new Singleton();

But in many tutorials available on internet like on wikipedia http://en.wikipedia.org/wiki/Singleton_pattern
protected constructor is used with comment // Protected constructor is sufficient to suppress unauthorized calls to the constructor
Please clear my doubt why they have used protected constructor.
Is it correct to use protected constructor in a Singleton class?

Thanks,
Anuj

 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I know that if we use protected constructor other classes can create Singleton instance using new Singleton();



There by you create more than one object, which means its not singleton anymore




Is it correct to use protected constructor in a Singleton class?


I m not sure of the authenticity of the article . but its very bad practise to provide protected constructor just for the sake of inheritance.


 
Anuj Prashar
Ranch Hand
Posts: 99
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying

But if using protected as a modifier for constructor is a bad practice why people use protected
and what is the meaning of this reason they have given-- Protected constructor is sufficient to suppress unauthorized calls to the constructor
Thanks
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anuj Prashar wrote: what is the meaning of this reason they have given-- Protected constructor is sufficient to suppress unauthorized calls to the constructor
Thanks




By providing protected constructor , you can limit your class to be instantiated only by its subclasses.
Well the statement (in wiki) is true about "protected" access specifier , but certainly not with "singleton" and "protected" together.
 
Anuj Prashar
Ranch Hand
Posts: 99
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again for your reply

But still my doubt is unclear that

why they have used protected constructor in Singleton class?
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The choice of using the protected access modifier is totally upto the implementer and is not mandatory.
If you foresee someone sub-classing your singleton then you should provide a protected constructor otherwise it would be impossible to subclass it.
On the other hand, if you are sure that nobody will ever subclass your singleton, then you should specify a private constructor.

Having said the above, we at javaranch do not really suggest using Singleton pattern, for reasons see this faq entry: http://faq.javaranch.com/java/SingletonPattern
 
reply
    Bookmark Topic Watch Topic
  • New Topic