• Post Reply Bookmark Topic Watch Topic
  • New Topic

initialization  RSS feed

 
saravanan ragunathan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

output: b = 0

i heard that when we don't specify the constructor
compiler creates the default constructor which intializes
instance variables ...but in this example i created the constructor
and initialized variable a and i didn't initialize variable b..
when i invoke obj.b ..it gives 0 (default value) ..
so where b gets initialized....

 
Stephan van Hulst
Saloon Keeper
Posts: 7932
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static and member variables are always initialized to 0, null, false, etc. before any constructors are executed.
 
Campbell Ritchie
Marshal
Posts: 56221
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saravanan ragunathan wrote: . . . compiler creates the default constructor which intializes instance variables . . .
Whoever told you that was mistaken. The default constructor does nothing at all. As Stephan van Hulst said, that leaves the default values unchanged.
 
saravanan ragunathan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Static and member variables are always initialized to 0, null, false, etc. before any constructors are executed.


where these variables gets initialized to default values..using default constructor?
 
Stephan van Hulst
Saloon Keeper
Posts: 7932
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. When you declare a static or member variable, it will have this form:

<Type> <Name> = <Value>.

The value is assigned to the variable before any constructor or initializer is ran.
If you don't supply the = <Value> part, the compiler will more or less add it for you, setting the variable to 0, null, false, etc.
 
Campbell Ritchie
Marshal
Posts: 56221
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saravanan ragunathan wrote: . . . where these variables gets initialized to default values..using default constructor?
Did you read my earlier posting?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The default constructor does nothing at all.


<pedantic>
It calls its parent's no arg constructor
</pedantic>
 
Campbell Ritchie
Marshal
Posts: 56221
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I take your point, Joanne, but to be pedantic on my part it's a superclass not parent
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I take your point, Joanne, but to be pedantic on my part it's a superclass not parent


Fair enough. We'll call it a draw this time
 
Campbell Ritchie
Marshal
Posts: 56221
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll be happy with a draw; I usually lose to you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!