Instance initializer block
Instance Initializer block is used to initialize the instance data member. It run
each time when object of the class is created.
The initialization of the instance variable can be done
directly but there can be performed extra operations while initializing the
instance variable in the instance initializer
block.
Fig: Instance initializer block |
Example of instance initializer block
class Bike7{
int speed;
Bike7(){System.out.println("speed is "+speed);}
{speed=100;}
public static void main(String args[]){
Bike7 b1=new Bike7();
Bike7 b2=new Bike7();
} }
Output:
speed is:100
speed is:100
class Bike7{
int speed;
Bike7(){System.out.println("speed is "+speed);}
{speed=100;}
public static void main(String args[]){
Bike7 b1=new Bike7();
Bike7 b2=new Bike7();
} }
Output:
speed is:100
speed is:100
No comments:
Post a Comment