aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructors" Watch "Constructors" New topic
Author

Constructors

Sam Cooper
Greenhorn

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

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.


Rob
SCJP 1.4
Valentin Crettaz
Gold Digger
Sheriff

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...


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[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
Sheriff

Joined: Aug 26, 2001
Posts: 7610
choi,
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: http://aspose.com/file-tools
 
subject: Constructors