java constructor
Whenever we are creating an object some part of the java program(code) will be executed automatically to perform initialization of the object, this part of the program is called "Constructor".
Hence the main objective of the constructor is to perform initialization of an object.
example 1.
class Employee
{
String name;
int eid;
Employee (String name, int eid)
{
this.name = name;
this.eid = eid;
System.out.println(name+"-----"+eid);
}
public static void main(String args[ ])
{
Employee e1 = new Employee("Dhoni",101);
Employee e2 = new Employee("Virat",102);
}
}
Output of the above program is:-