This question made me reflect a bit, so i tried this little program. But.. the result is not what i was expecting (based on the explanation given in the answer of the exercise).
Here is my code:
Code: Select all
class AClass
{
AClass(){System.out.println("initialized AClass");}
static void method(){System.out.println("AClass method");}
}
class Prove1
{
Prove1(){System.out.println("initialized Prove1");}
public static void main(String[] args)
{
Prove1 p = new Prove1();
AClass.method();
}
}
I thought that invoking AClass.method() would create a new AClass instance, and so the AClass constructor would be called. But it's not so.As per JLS 12.4.1 - A class or interface type T will be initialized immediately before the first occurrence of any one of the following:
...
T is a class and a static method declared by T is invoked...
So, i don't understand the explanation given.
p.s. the result is:
initialized Prove1
Base method