This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

a class with private constructor ??

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it, thank u.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic