| Author |
When is a class loaded?
|
Sandra Bachan
Ranch Hand
Joined: Feb 18, 2010
Posts: 434
|
|
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
|
Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
|
 |
Hauke Ingmar Schmidt
Rancher
Joined: Nov 18, 2008
Posts: 371
|
|
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
Joined: Feb 18, 2010
Posts: 434
|
|
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
Joined: Feb 18, 2010
Posts: 434
|
|
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
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
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;...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Diana Sule
Greenhorn
Joined: Nov 08, 2010
Posts: 21
|
|
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".
|
OCPJCP 6.0 | OCPJWCD 5 | SpringSource Certified Spring Professional - Spring 3.0
|
 |
Hauke Ingmar Schmidt
Rancher
Joined: Nov 18, 2008
Posts: 371
|
|
|
I am sorry, what I wrote seems to be incorrect. I apologize.
|
 |
Deepak Bala
Bartender
Joined: Feb 24, 2006
Posts: 6588
|
|
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.
|
SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
|
 |
Trivikram Kamat
Ranch Hand
Joined: Sep 26, 2010
Posts: 155
|
|
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
|
OCPJP6
|
 |
Hauke Ingmar Schmidt
Rancher
Joined: Nov 18, 2008
Posts: 371
|
|
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
Joined: Sep 26, 2010
Posts: 155
|
|
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
Joined: Nov 02, 2010
Posts: 87
|
|
This will also present Trivikram Kamat idea
|
love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
|
 |
 |
|
|
subject: When is a class loaded?
|
|
|