• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private Constructor

 
Don Bosco
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic