File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When is a class loaded?

 
Sandra Bachan
Ranch Hand
Posts: 434
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • 1
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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!!!

 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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;...
 
Diana Sule
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry, what I wrote seems to be incorrect. I apologize.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • 1
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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