• 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

confusion about Static Block...actually its about class loading

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

I have a confusion about Static Block...actually its about class loading
Here is the coad




In the above code if I make the #Line 1,2,3 commented-un commented one by one..then results are
Static Block Called...when only #Line 1 is un commented
Static Block do not Called...when only #Line 2 is un commented
Static Block Called...when only #Line 1 is un commented

result for #Line 1,3 are obvious and same as expected...
but for #Line2 i was expecting that Static block would be called..
Isn't it mean that any class would not get loaded until we create its object or 1st time access of any static variable of that class.
And more with this JVM will checks only the availability of the class when we say 'Object Obj = null;' , it don't load the class.

is my assumption is OK ??
anybody wants to put comment??
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I knew the answer would be hidden somewhere inside the compilation so I decompiled StaticBlockTest after only uncommenting line 2 and this is what the result was



Since you didn't call anything on the object of StaticBlockClass, so the compiler converted the statement and thus, StaticBlockClass was not loaded...
 
vijay saraf
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats Strange....Why that idea didn't click in my mind...any way...
If After compilation i delete the class file of StaticBlockClass and run the program...then I should not get any error related 'NoClassDefFound'.
But it is...and it must be...
I think Compiler may clip some more information related to reference type change...

 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I compiled the class with only line 2 uncommented, then deleted the StaticBlockClass.class file and then gave the command java StaticBlockTest and I didn't get any NoClassDefinitionFoundError...
 
vijay saraf
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any way Ankit....your right !
but what about my asumption...is it OK...

Isn't it mean that any class would not get loaded until we create its object or 1st time access of any static variable of that class.
And more with this JVM will checks only the availability of the class when we say 'Object Obj = null;' , it don't load the class.

 
reply
    Bookmark Topic Watch Topic
  • New Topic