Wednesday, 18 May 2016

Constructor in java

Whenever  we’re discussing about classes, one of the most important      subtopic  would be  constructor . Constructor is a special type of method, which name is exactly same as the class name.
·         Every class has a constructor if we do not explicitly write a constructor for a class the java compiler builds a default constructor for that class.
·         Each time a new object is created, at least one constructor will be invoked. The main rule of constructor is that they should have same name as class name.
·         Constructor does not have any return type.
constructor is use for the object initialization.

There are two types of constructor:




pubic class puppy{
public puppy(){
 //statement 
}

public Puppy(String name)
{
//statement
}
}
public static void main(String args[])
{
Puppy t1= new Puppy("tommy");//paramaterized 
Puppy t2 =new Puppy();//default
t2.Puppy();
}
}

0 comments:

Post a Comment