• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

static initiatization for class and interface

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

Output is
j=3
jj=4
j1=3
jj1=4
3

now change interface to class

output is
ii=2
j=3
jj=4
3

why such a difference? explanation give is
A class or interface type T will be initialized at its first active use, which occurs if:
T is a class and a method actually declared in T (rather than inherited from a superclass) is invoked.
T is a class and a constructor for class T is invoked, or U is an array with element type T, and an array of type U is created.
A non-constant field declared in T (rather than inherited from a superclass or superinterface) is used or assigned. A constant field is one that is (explicitly or implicitly) both final and static, and that is initialized with the value of a compile-time constant expression . Java specifies that a reference to a constant field must be resolved at compile time to a copy of the compile-time constant value, so uses of such a field are never active uses.
All other uses of a type are passive
A reference to a field is an active use of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface. Initialization of an interface does not, of itself, require initialization of any of its superinterfaces.

i didnt get it. pls help
Aruna

[ March 26, 2005: Message edited by: Aruna Agrawal ]
[ March 27, 2005: Message edited by: Aruna Agrawal ]
 
Aruna Agrawal
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is any one listening..
pls reply
 
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 Aruna,

for reference you can refer one of the posts made by me only where i'd jotted down this concept from the JLS, so you can look at the points first.
https://coderanch.com/t/248461/java-programmer-SCJP/certification/compile-time-constant-static-vaiable

in ur first example since u've made I and J as interface so by default all the member variables or fields will be public static final, so in interface j fields j1, jj1 are also static.
now from ur class Test you are acessing j which is origianlly in interface J, we can that j is not a compile time constant so here this Inerfaced will get initialized
[JLS says]


(Initialization of a class consists o executing its static initializers and the initializers or the satic fields(class variables) declared inthe class . Intialization of an interface consists of executing the initializers for fields(constants) declared there.
Before a class i initialized, its superclasses mus tbe initialized, but interfaces implemented b the class are not initialized. Similarly, the superinterfaces of an interface are not initialized before the interface is initialized.)


and so I will not get initialized.
now initialization of all static feilds will happen in the interface J so
first in o/p you get j=3 then jj=4, then j1=3 then jj1=4 and finally your access j call in class Test prints o/p as 3

In ur second modified code:
You have made I and J as classes so now if we're initializing class J then class I also should be initialized before that so now when you access j which is originally in J all static fields should be initialized and since I is the supeclass, firstly static fields of I will be initialized so you get the o/p as:
ii=2 then j=3 then jj=4 finally 3 is printed because of your access statement is inside the println().

hope this is clear enough. if not feel free to get back .....
 
Aruna Agrawal
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks amit ..
i knew all the facts you told .what i dont know is why . why the different treatment is given to Class and interface in the above context

to make my question more clear,
1) why superinterface are not initalized and why superclass are initalized on active use of sub-interface / sub-class respectively.
2) Why interface which a class implements is not initalized on active use of the class( this question i got from you reply :roll: )

luv
aruna
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aruna,
Good question!!
I dont have any clear idea why this happens, but i have a say on this which might be appropriate.

See before u call any method on the class it should be initialized. Now u know that the Object is the supreme being. Supreme being in the sense that this class should be initialized before any class gets initialized. Then only u can call methods on object class, like finalize(), notify(), wait()...etc(think logically).
So if Object has to be initialized then the initialization path to be followed should also initialize the other super classes which comes on the way. For eg.


So here since Object class has to be initialized, the path to be followed should be:
1) Initialization of Object class
2) Initialization of A class
3) Initialization of B class
4) Initialization of C class

So u agree till here?
Now lets look at the interfaces. DO u have any SUPREME BEING INTERFACE, Nooooo
U have no such interface which every class should implement directly or inidrectly. Only when u need the interface u implement it.
So, why to initialize until we need.
Thats the main intent what i believe.

I hope i am making sense

Thanks
 
Aruna Agrawal
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Animesh Shrivastava:
Only when u need the interface u implement it.
So, why to initialize until we need.
Thats the main intent what i believe.



The above logic of yours apply to class also... if i am just refering a variable of a class, then intialize only that class.. why unnessary initalize its superclass way to Object class.. when i use any function of superclass themn intialize that class...if i use notify or wait then initialize object class till thendont inialize the way we do for interfaces..




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

There may be some situation where a method in ur subclass may access a property in your superclass. So for the code to work now all the superclass properties should be initialized.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By Aruna,


The above logic of yours apply to class also... if i am just refering a variable of a class, then intialize only that class.. why unnessary initalize its superclass way to Object class


Well Aruna, When u extend any class, the super class's accessible members also become members of the sub class. Thats how u can just invoke any instance method of super class from ur sub class instance method. This implies that the object's state also comprises of super class's state.

But interface is not this way. It just defines constants in it. So when we need those the state of the interface gets initialized.
 
Aruna Agrawal
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ajay Bhargov:
Hi Aruna,

There may be some situation where a method in ur subclass may access a property in your superclass. So for the code to work now all the superclass properties should be initialized.



similar situation can exist in interfaces as well
 
Aruna Agrawal
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Animesh Shrivastava:
When u extend any class, the super class's accessible members also become members of the sub class. Thats how u can just invoke any instance method of super class from ur sub class instance method.



now lets see i change your statment .. replace class with interface and remove word instance...
so hows this
When u extend any interface, the super interface's accessible members also become members of the sub interface. Thats how u can just invoke any method of super interface from ur sub interface method.
:roll: :roll: :roll:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic