• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a class with private constructor ??

 
Peixiao Lin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only public or default can apply to class, but what about a class with private constructor? I tried it and seems it is not accessible outside of class. If so, what is this class for? useless? Please help me out. Thanks for reply.
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make the constructer private, if you don't want others to create any objects of that class.
It doesn't mean that the class is useless. For example the java.lang.Math class has a private constructer, but still we can use it (with out creating any objects).
Also, making constructers private is one approach to implement Singleton pattern.
 
Peixiao Lin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it, thank u.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some classes have special static methods that will create an object of the same class for the caller. These are known as factory methods. Only the factory method is allowed to construct an object of the class, so in this case the constructor would be made private to the class.
[ October 07, 2004: Message edited by: Barry Gaunt ]
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having only private constructors to a class will also make sure the class can never be subclassed.
This is a good way to enforce limitations to inheritance.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeroen Wenting:
Having only private constructors to a class will also make sure the class can never be subclassed.
This is a good way to enforce limitations to inheritance.


The standard way, though, is to simply declare the class as final.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct way is both.
A class with a private ctor is implicitly unsubclassable.
An acknowledgement of this design decision by explicitly declaring the class 'final' is warranted.
Most style checkers (that I know) of will pick this one up.

"classes are public or default".
For the purposes of the exam, it might pay to point out that "top-level classes are public or default". Nested classes can be and one of the four access scopes.
 
james edwin
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can also use private constructor in class when we want to implement Singleton Design Pattern(Only one instance of that particular class).
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A more general statement might be "A private constructor allows control over the construction of instances". The Factory and Singleton demand control over the construction of instances, as do many other design patterns.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic