| Author |
wrapper's initial value
|
Michelle Chang
Greenhorn
Joined: Jan 22, 2010
Posts: 8
|
|
Here is part of code somewhere in KB's book.
Just a small question here. Does static variable get its initial value when the class is loaded? If that is the case, my next question is that is i's initial value in line 3 null because it's a wrapper class?
Thanks.
|
Always speak up for stupid questions.
|
 |
Larry Olson
Ranch Hand
Joined: Feb 03, 2009
Posts: 142
|
|
Yes, all static variables get initialized when a class is loaded. Wrapper variables are initialized to null, since it is a non-primitive type.
I would strongly recommend that you install jdk on your computer/laptop or whereever and start typing this code, compile it and execute it for yourself to see the results - in this case confirm that the Integer variable indeed gets a null value. Try to do a demo code for every concept you learn and execute it. You will enjoy it and believe me, you will not forgot the concept easily.
|
 |
Prasad Kharkar
Ranch Hand
Joined: Mar 07, 2010
Posts: 435
|
|
and yes, I would also like to tell you that static variables are completely related to class and they have nothing to do with objects
they are initialized when the class first loads
and about the instance variables, they are given the default values when the object is created and it calls the respective constructor
|
SCJP 6 [86%] June 30th, 2010
If you find any post useful, click the "plus one" sign on the right
|
 |
Prithvi Sehgal
Ranch Hand
Joined: Oct 13, 2009
Posts: 767
|
|
Please quote the source for the above mentioned source code.
Regards,
|
Prithvi/Beenish,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
|
 |
Afzal Rehman
Ranch Hand
Joined: Dec 16, 2009
Posts: 35
|
|
static variable initialized when class loads
Output will be 0.
but if you try to print i as an instance variables initialized by default values when instance created
see the example.
it will produce Output 0.
but look at this example ...
This will produce an Compiler error int i is being not initialized. so keep concentration when you are using variables in a methods
those being initialized or not...
|
SCJP 6.0
|
 |
 |
|
|
subject: wrapper's initial value
|
|
|