wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Private Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Private Constructor" Watch "Private Constructor" New topic
Author

Private Constructor

Don Bosco
Ranch Hand

Joined: Oct 31, 2002
Posts: 108
class A{
private A(){
System.out.println("private");
}
public static void main(String[] args) {
A a = new A();
}
}
The above code works fine. A class with private constructor can be instantiated?? Can anybody explain me?


SCJP 1.4<p>Wingardium Leviosa!!
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

A class with private constructor can not be instantiated outside the class, it can be instantiated only from the class itself. Your main method is inside the class, hence...
HTH,
- Manish
Don Bosco
Ranch Hand

Joined: Oct 31, 2002
Posts: 108
Thanks!!
 
wood burning stoves
 
subject: Private Constructor
 
Similar Threads
Private Constructor
Extending a Singleton Class
Marcus exam Q30
singleton class
Query on constructor