Win a copy of Microservices Testing (Live Project) this week in the Spring 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Private Constructors

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can somebody pls explain what the effects of having a private constructor //1. What is the significance. I notice the code still compiles. But is it still possible to instantiate B. Will constructor chain still execute properly?
[This message has been edited by Pervez Amin (edited November 09, 2001).]
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But is it still possible to instantiate B?
[/b]
</BLOCKQUOTE>
Yes, you can instantiate an object of class B. B has no explicit constructor, so the compiler inserts a default no argument constructor. That constructer then invokes the explicit no argument A constructor. No problem.

You could, however, have the following class:


[This message has been edited by Bill Krieger (edited November 09, 2001).]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
normally you decide to make a constructor private when you want your class to be in control of how much instance can be created. This is used in the Singleton Design Pattern that enforces that only one instance of a class will be created. The constructor is made private and the class provides a static method (getInstance for instance) which will return an instance when asked. The first time it will call the constructor to create an instance which is stored as private member within the class. Subsequent invocation of the getInstance method will return the reference to the only instance of the class.
Another use of a private constructor is when you don't want any instance of your class to be created. See the Math class which has only static methods and a private constructor...
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Sheriff
Posts: 17031
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since class B does not have an explicit constructor, the compiler creates an implicit no-args constructor with default (package/friendly) access (since class B has default access). This does not prevent the normal constructor calls from happening when you instantiate C. It also means that you can still instantiate B from any class belonging to the same package.
If, however, you had declared a private constructor for class B, you would break class C because it will not be able to make an explicit or implicit call to super() from any of its constructors.

------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic