• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

****Difference in Intialization process of a class and an interface???*****

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

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:



O/p is:

Learn and keep learning!!!
1
j=3
jj=4
3

If i change the code like :


O/p is:

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
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?


thanks in advance
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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.

thnx
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amit,
In the class version
ii=2 is getting printed when u try to print K.j
not when uu try to print I.i

When K.j a runtime value not a compile time constant is accessed the super class variable ii gets initialized.

It doesnt happen with the interface version..
 
achayya matta
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai Amit,

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!

thank u
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well well well!!!

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.

Regards,
Amit
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic