File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in constructor access modifier... 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 "doubt in constructor access modifier..." Watch "doubt in constructor access modifier..." New topic

doubt in constructor access modifier...

Erum Momin
Ranch Hand

Joined: Jun 10, 2009
Posts: 32
Hi All,
Can anybody please tell me... what is ruling for access modifiers of the constructor...
A constructor must have the same access modifier as it's class?
Please help me....
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

A constructor can have any modifier. Its rules are same as using access modifiers for methods. there is no rule that the access modifier of the class and its constructor should be same. Class can have only public and default access modifiers while constructor can be protected public private and default.

[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
No, the classes' and constructors' access modifiers share no connection.

Usually you would mark your constructor as public, so any class can build an instance of it.

The best example not to do this though is the Singleton pattern.
In a Singleton you mark the constructor as private, so nobody but your own class can instantiate it.

To provide an instance to other classes, you would create a public static function that builds the instance on demand.
This allows you to control instances and impose restrictions, in the case of the Singleton you would only allow one instance.

JDBCSupport - An easy to use, light-weight JDBC framework -
I agree. Here's the link:
subject: doubt in constructor access modifier...
It's not a secret anymore!