Thursday, March 12, 2009

How the private constructor is called in the main java

Question :How the private constructor is called in the main java
programme? (CoreJava)

Answer :Have a look at this demo. public class Test2
{
private Test2()
{
System.out.println("Test2class");
}
class Subclass extends Test2
{
public Subclass()
{
System.out.println("Subclass");
}
}
public static void main(String[] args)
{
Subclass s = new Test2().new Subclass();
}
}
This works because an inner class is allowed to access private members of
its enclosing instance, including the private constructor.

No comments: