Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how do variables get initialized when a blank constructor is provided

 
VamC Krishna
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm....
anyone know the answer ?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry,
I overlooked the section 4.12.5 part in your message, I was asking that.
I am reading that now ..
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic