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
Hi, Actually, since main method is declared as static, it tends to take it into account. First, static initializers are performed and then, the main method which is also static is called. Hence the output. Consider the following case for a better explanation: <CODE> public class base { public static void main(String args[]) { System.out.println("In base!"); } } </CODE> and <CODE> public class derived extends base { static { System.out.println("Derived!"); } } </CODE> The output is: Derived! In base! Hope this makes things clear. Aparna
Aparna If so, will it call any static method called or only main method? Anitha
Sudha I tried running just the innerclass but it always gives the exception in thread main Nosuchmethod error I tried something like public class Test { public class Inner1{}; public static class Inner2{} } java test$Inner1 java Test$Inner2 <Quote> if you run obj1$obj2 you get </quote> can you or somebody tell me why I am getting an exception Thanks Anitha
[This message has been edited by Mary Anitha (edited November 16, 2000).]
Thanks Aparna, So that means eventhough say class obj3 doesn't have a main method it still executes the main method of it's super class after doing the static initializers. I don't think it will call other static methods of super class, just the main method. Am I right? Thanks Sudha
Hi Sudha and Mary, Sorry, was in a meeting, hence was not able to reply immediately. Anyway, here goes my explanation: As soon as you try running a java program and say java myprogram, it looks for a main function with the proper signature. If it doesn't find it within this function, it has to look into the superclass of this so that it can use that main function. Sudha, in your program itself, if you don't have a main function in your base class, then, it would have given a runtime exception of NoSuchMethodError called main. Now, lemme give you another set of base and derived classes, wherein the main function resides in both these classes. In such a case, the main function that resides in derived class alone is executed though a main function resides in base class too. And one more thing is that, you can access any static method of your base class in your derived class. (mymethod() in my example here) Look out for this code: <CODE> public class base { public static void mymethod() { System.out.println("In mymethod()"); } public static void main(String args[]) { System.out.println("In base!"); } } </CODE> and <CODE> public class derived extends base { static { System.out.println("Derived!"); } public static void main(String args[]) { mymethod(); } } </CODE> When you run derived.java, it gives Derived! In mymethod() as output Aparna