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.
This message was edited 1 time. Last update was at by Michelle Chang
Always speak up for stupid questions.
Larry Olson
Ranch Hand
Joined: Feb 03, 2009
Posts: 142
posted
0
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.
This message was edited 1 time. Last update was at by Larry Olson
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: 735
posted
0
Please quote the source for the above mentioned source code.
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...