First, thanks all for your help and support..I highly appreciate it
I have modified the code posted in one of the posts above a little as shown below
<blockquote>
code:
<pre name="code" class="core">
public class Dog extends Animal{
int i = f(1);
{ System.out.println("2_SUBClass"); }
int j = f(3);
{ System.out.println("4_SUBClass"); }
static { System.out.println("instatic SUBClass"); }
static int m = fstatic(5);
int f(int k){
System.out.println(k + "_SUBClass");
return k;
}
static int fstatic(int k){
System.out.println(k + "_SUBClass_Static_VAR");
return k;
}
public static void main(String[] args){
new Dog();
}
}
class Animal {
int iA = fA(1);
{ System.out.println("2.SUPER"); }
int jA = fA(3);
{ System.out.println("4.SUPER"); }
static { System.out.println("instatic SUPER"); }
static int mA = fstaticA(5);
int fA(int k){
System.out.println(k + ".SUPER");
return k;
}
static int fstaticA(int k){
System.out.println(k + ".SUPER_Static_VAR");
return k;
}
} </pre>
</blockquote>
and by executing
java Dog
I got
instatic SUPER
5.SUPER_Static_VAR
instatic SUBClass
5_SUBClass_Static_VAR
1.SUPER
2.SUPER
3.SUPER
4.SUPER
1_SUBClass
2_SUBClass
3_SUBClass
4_SUBClass
which confirms that all static variables, blocks for all classes in the inheritance tree of the first initialized class (Dog) will run first, then instance variable and blocks will follow from the top down in the inheritance tree
So far so good, but when I tried changing the code again and making it as follows:
<blockquote>
code:
<pre name="code" class="core">
public class Dog2 extends Animal{
int i = f(1);
{ System.out.println("2_SUBClass"); }
int j = f(3);
{ System.out.println("4_SUBClass"); }
static { System.out.println("instatic SUBClass"); }
static int m = fstatic(5);
int f(int k){
System.out.println(k + "_SUBClass");
return k;
}
static int fstatic(int k){
System.out.println(k + "_SUBClass_Static_VAR");
return k;
}
public static void main(String[] args){
new Dog2();
}
}
class Animal {
int i = f(1);
{ System.out.println("2_SUPER"); }
int j = f(3);
{ System.out.println("4_SUPER"); }
static { System.out.println("instatic SUPER"); }
static int m = fstatic(5);
int f(int k){
System.out.println(k + "_SUPER");
return k;
}
static int fstatic(int k){
System.out.println(k + "_SUPER_Static_VAR");
return k;
}
}
</pre>
</blockquote>
and by executing
java Dog2
I got the following result:
instatic SUPER
5.SUPER_Static_VAR
instatic SUBClass
5_SUBClass_Static_VAR
1.SUPER
2.SUPER
3.SUPER
4.SUPER
1_SUBClass
2_SUBClass
3_SUBClass
4_SUBClass
So I was wondering, shouldn't inheritance have played a role here especially that there are static and instance methods and vars with the SAME EXACT name?
Thanks in advance for your help