• 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

When is a class loaded?

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought class was loaded with a reference variable points to an object of a class on the HEAP.

However, according to Enthuware, it is when a reference variable of the class is declared.

Please see below question, answer and explanation:

Carefully examine the following code.




What will be the output?



My answer was:


1, In static, In non - static, 2, In non - static : in that order.


Apparently that is incorrect. Correct answer is:


In static, 1, In non - static, 2, In non - static :in that order.

The "1" is switched with "In static"


Please explain what is happening
 
Rancher
Posts: 436
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The static block will be executed after the class has been loaded.

The class must be loaded for the declaration of a reference variable.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hauke Ingmar Schmidt wrote:The static block will be executed after the class has been loaded.

The class must be loaded for the declaration of a reference variable.



This helps!
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I came across a similar problem in Enthuware, applied the same logic, and got the answer wrong:


What will be output when class Test is run:





My answer was:

It will print one, super and two



1. When the reference variable of type One is declared, its static blocks are initialized, i.e

static { System.out.print("one "); }

will print "one"

2. When reference variable of type Two is declared and instance of Two is created on the heap, i.e.

Two t = new Two();

Super's static block will execute, outputting "super"

3. Then Two's static block will execute, outputting "two"

Yet, my answer is wrong....

The explanation is

"one" will not be printed as class One is not actively used.



Which leaves

It will print super and two.




Please explain!!!

 
Sheriff
Posts: 9708
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
These types of questions are very complicated. This behavior depends on compiler optimizations. Since the reference variable o is not used, so the compiler converts it into Object o = null;...
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandra,

Two t = new Two();

In the Two() constructor, it will first call the super() - in this case - the Super class - because Two extends Super - this will print "super" and then the constructor of Two continues and prints "two".
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry, what I wrote seems to be incorrect. I apologize.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The class must be loaded for the declaration of a reference variable.



I used to think this was right. While debugging a class loader problem, it turned out that the JVM I was running did not load classes unless they needed to be instantiated or a static call was made. When a reference was encountered the class was not loaded.

I am not sure if there is a spec out there that mandates when a class should be loaded. I would question if that needs to be part of a spec either. So as to the correctness of any answer, I would not be too worried since SCJP does not expect you to know when a class is loaded. It expects you to know "how" a class is loaded and what gets called in order.
 
Ranch Hand
Posts: 155
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandra Bachan wrote:
The "1" is switched with "In static"



"In static" is printed first, because class StaticTest is loaded when you call it by using command:
java StaticTest

The following code will print "1" first, as class StaticTest is loaded at 2
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Trivikram Kamat wrote:
"In static" is printed first, because class StaticTest is loaded when you call it by using command:
java StaticTest



D'Oh.

Thank you.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code below, 0 is printed after "In static"



This explains that "In static" is printed on calling StaticTest, and not while declaring StaticTest object.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This will also present Trivikram Kamat idea
reply
    Bookmark Topic Watch Topic
  • New Topic