Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How Instance variables get Default values

 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When the instance variables will be initialized to default values?

when the class is loading , then it will intialize all its instance variables with Default values ? These will be intialized in the Defalut constructor ?( Class has defalut constructor , which intializes all the instance variables , regardless of values assigned in class or not by using constuctor) ?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this article: Object Initialization in Java.
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi marc,

My doubt is that , i have written one constructor to intialize the values

as

class Myclass
{
int var1;
int var2;
int var3 = 20;
Myclass(int v1, int v2)
{
varl= v1;
var2 = v2;
}

}

and i created object as

Myclass myclass = new Myclass(10,20);

for this object myclass values are var1 = 10 and var2 = 20, var3 = 30;

How many times values for var1 and var2 are intialized . Means , var1 and var2 ,var3 are initialized to default values(zero) when class is loaded.And after that var3 is intialized to 30 for myclass ?

please clarify me .

thanks in advance
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic