Wednesday, October 11, 2006

Java : fun with main()

Yesterday I was playing with main()... We have 1 class A (A.java) :

package com.mycoolcompany.mainAB;
class A
{
public static void main(String args[])
{
System.out.println("A");
}
}

Second class B.java :

package com.mycoolcompany.mainAB;
class B
{
public static void main(String args[])
{
(new A()).main(args);
}
}

It seems to be that java is comfortable with executing main() from another class - executing B says A ! Next thing is more interesting...
C.java :
package com.mycoolcompany.mainAB;
class C extends A
{
public static void main(String args[])
{
(new C()).main(args);
}
}

If we run this class, we'll have a stack owerflow. But it's compiled without problems ! And at last... Can we run class without main ? Maybe yes :)
package com.mycoolcompany.mainAB;

public class D extends A{}

Executing "java com.mycoolcompany.mainAB.D" says A !

No comments: