I was jsut trying various combinations of classes, inner classes & subclasses. I made a class obj1 with an inner class obj2. Then made a class obj3 which is a subclass of obj1 with a static code block.Execute this program & see what happens.
public class obj1
{
int i =2;
public static void main(
String args[])
{
int j=2;
obj1.obj2 o2 = new obj1().new obj2();
o2.methodA();
System.out.println(o2.h);
}
class obj2
{
String h ="Hello";
void methodA()
{
String s = "Java ";
System.out.println(h+s+i);
}
}
}
class obj3 extends obj1{
static{
obj1.obj2 o4 = new obj1().new obj2();
o4.methodA();
System.out.println(o4.h+="SCJP");
}
}
The output of the program when you run
java obj1
is
Hello Java 2
Hello
if you run obj1$obj2 you get
Hello Java2
Hello
& when you run java obj3 you get the following o/p
Hello Java 2
Hello
SCJP Hello Java 2
Hello
My doubt is from where does this last 2 lines come from?
See neither obj1 or obj2 have any constructors then how does it execute the method call in main of obj1.
Can anybody clear my doubt?
Thanks in advance
Sudha