Can anyone bother to explain me a thing which is disturbing me a lot:
Hereunder i'll present 2 snippets. The difference in them will only of a class and an interface. Anyways, I'm highlighting the doubtful area:
Learn and keep learning!!! 1 j=3 jj=4 3
If i change the code like :
Learn and keep learning!!! 1 ii=2 j=3 jj=4 3
I guess the question is quiet clear now. Can anyone explain the initilization happens?
I quote an excerpt from the Initialization section of The JLS2.0
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 an instance of T is created. T is a class and a static method declared by T is invoked. A static field declared by T is assigned. A static field declared by T is used and the reference to the field is not a compile-time constant (�15.28). References to compile-time constants must be resolved at compile time to a copy of the compile-time constant value, so uses of such a field never cause initialization
Plz take this into consideration when answering. Regards, Amit
hai can anyone tell me when and how a class or interface gets loaded and what exactly happens when a class gets loaded? what is the difference between class getting initialized and class getting loaded?which of these happens first?
Nice Topic.Thnx Amit. I also have some doubts on it.
======================================= My understanding is
Class loading means, When any of the followings happen (as Amit mentioned)
1) Class.forName("class name") is called. 2) The class is being extended by a class that is being loaded. 3) Any object of the class is being created. 4) Any static method of the class is being called. 5) Any static field of the class is being initialised. 6) Any static field of the class is being referenced somewhere. But keep in mind that the class will not be initialised after referenceing a static final field of that class.
Now come what will happen when a class is being loaded. 1) All static fields(class variables) will be initialised. 2) All static blocks will be executed.
But i have doubts on class initialising. May be it will be initialising an object of the class.That can be done by simply calling a constructor of the class. Once an Object of the class is initialised then apart from the thing that will happen when a class is loaded as i mentioned above
1) super() will be called from aonstructor. 2) All the instance variable will be initialised. 3) Constructor body will be executed.
Thats all i know....please if i am wrong rectify me... And als let me know above things in case of Interface.
i spent more than 30min on ur question and eventually that query taught me many concepts about class or interface initialization.The problem is i understood whats happening in that question but i cant express it in words. i dont know why.
here i am making a small attemt to convince u
The two main points that JLS2.0 is emphasizing are:-
1).A static field declared by T is used and the reference to the field is not a compile-time constant.
2).Before a class is initialized, its direct superclass must be itialized, but interfaces implemented by the class need not be initialized. Similarly, the superinterfaces of an interface need not be initialized before the interface is initialized
"ii=2" is not printed in ur first code becoz of the above first point.
Why "ii=2" is printed in ur second code is becoz of the above second point.
i tried my best.
better explanation is welcomed
Amit its a very good question and keep posting such questions!
Thankyou all for showing interest in the question. I know many of us (the RANCHERS) think that the problem is irrelevant, but if someone reads b/t the lines, actually its not and as mentioned by Ashok, who has bothered himself to do that, it clears lot many concepts.
I thank to Ashok for providing the correct answer. Ashok, you are right, actually the answer itself lies in the excert provided by me from The JLS2.0.