aspose file tools*
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
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: 2579

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!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Private Constructor