This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructors" Watch "Constructors" New topic


Sam Cooper

Joined: Feb 21, 2002
Posts: 14
Can the constructors be marked private or protected or friendly (none). If so whats their significance.
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
Yes. The access modifiers on constructors will work the same as when they are used with methods.
If you mark a constructor private, only methods within the class itself can use that constructor. That means code outside this class cannot instantiate any objects! Although this might seem useless, it is actually a very-well used technique when using the Singleton design pattern.
If you mark a constructor protected, then only subclasses of the class, or classes in the same package, can call the constructor. This is useful in situations where you may not want an outside method to instantiate an object of that class, but you want to be able to subclass the object, and construct a sublclass. The subclass now has access to the superclass constructor and can instantiate it. This is actually used in the io package - the no-arg constructor of ObjectOutputStream is marked protected, so you can't instantiate an ObjectOutputStream via that constructor. But you can create a subclass of it, and then call the superclass no-arg constructor.
Finally, default access is similar to protected above, but only methods in the same package as the object can use the constructor to instantiate a class of that type.

SCJP 1.4
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
yes they can.
the meaning is the same as for methods.
if a constructor is private that means that only the method within the class can call it, and thus, create an instance of the class. This is usually used to build so called Factory classes which have a static method like getInstance() or something that return an object of the class. That way you can control how many instances of your classes are created.
If it is default, then it may only be called by classes within the same package.
If it is protected, then it may only be called by classes within the same package and subclasses in other packages...

[Blog] [Blogroll] [My Reviews] My Linked In
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
I read somewhere that constructor can only be defined as public or protected, but when I tried to define a private constructor, it works!
I guess all the explanations below solved my query.

best regards,<br />Steven<br />SCJP, SCEA
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
when in doubt read the JLS... and the latter says in 8.8.3 Constructor Modifiers

ConstructorModifier: one of
public protected private
I agree. Here's the link:
subject: Constructors
It's not a secret anymore!