Constructor is a special method to initialize the object.
constructor is invoke (object using) at time when you have object creation.
Simple just Rules to create constructor in java.
1) Must same constructor name as class name.
2) Constructor must have no explicit return type. means that is specifier (static or inline) Like.
There are two type of constructor..
1)Default constructor. -- in default constructor no have any parameter. so that is default constructor.
ex. class demo{
demo(){System.out.println("hello");}
}
2)Parameterized constructor. -- Constructor have parameter that is parameterized constructor.
why is need to .. because you have provide a different values to object at invoking(object creation time) time.
ex. class demo{
int i;
demo(int n){
i = n;
System.out.println("n is -"+i);}
public static void main(String args[]){
demo d = new demo(10);
}
}
*************************************************************************
Comments
Post a Comment