| Author |
how do variables get initialized when a blank constructor is provided
|
VamC Krishna
Greenhorn
Joined: Jan 09, 2009
Posts: 5
|
|
The class or instance variables are initialized by the constructor.
If I write a constructor, the default constructor is not inserted by the compiler,
but even if I don't initialize the variables in the constructor , the variables still get default values ...how ?
Does the <init> method initialize them by itself ??
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
I always thought it was because the JVM deletes all the memory which the fields will occupy, when it instantiates the object.
But I might be mistaken.
|
 |
VamC Krishna
Greenhorn
Joined: Jan 09, 2009
Posts: 5
|
|
hmmm....
anyone know the answer ?
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
VamC Krishna wrote:The class or instance variables are initialized by the constructor.
No. Class or instance variables are initialized when they are created. You can use the constructor to change the default value.
See the Java Language Specification, Section 4.12.5.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
I thought your question meant, "How are the default values initialised?"
If you want to insert different initial values, there are several ways to do it; I would prefer to see them inside the constructor. Example:
|
 |
VamC Krishna
Greenhorn
Joined: Jan 09, 2009
Posts: 5
|
|
Campbell Ritchie wrote:I thought your question meant, "How are the default values initialised?"
If you want to insert different initial values, there are several ways to do it; I would prefer to see them inside the constructor. Example:
sorry !!
not really ..
consider the following Program
Now if I run the above program I get the output as
0
........
the integer i is initialized to 0.
As I wrote a constructor, the default no-arg constructor is not created by the compiler. Then who initialized the variable.
|
 |
VamC Krishna
Greenhorn
Joined: Jan 09, 2009
Posts: 5
|
|
Joanne Neal wrote:
VamC Krishna wrote:The class or instance variables are initialized by the constructor.
No. Class or instance variables are initialized when they are created. You can use the constructor to change the default value.
See the Java Language Specification, Section 4.12.5.
Oh ! thank you.
If possible, can you please give the page no. or chapter no. if it is readily availbable with you. Mean while I will keep searching it .
Thanks a lot
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
VamC Krishna wrote:
Joanne Neal wrote:
VamC Krishna wrote:The class or instance variables are initialized by the constructor.
No. Class or instance variables are initialized when they are created. You can use the constructor to change the default value.
See the Java Language Specification, Section 4.12.5.
Oh ! thank you.
If possible, can you please give the page no. or chapter no. if it is readily availbable with you. Mean while I will keep searching it .
Thanks a lot
Click on the link. There are then links in both the HTML and PDF versions to each of the sections.
|
 |
VamC Krishna
Greenhorn
Joined: Jan 09, 2009
Posts: 5
|
|
Sorry,
I overlooked the section 4.12.5 part in your message, I was asking that.
I am reading that now ..
thanks
|
 |
 |
|
|
subject: how do variables get initialized when a blank constructor is provided
|
|
|