class test4
{
int l;
int b;
test4() // default constructor
{ l=1;
b=2;
}
test4(int tl) // function name is same as class name
{ l=tl;
b=4;
}
test4(int tl,int tb) // class name same as function name with different parameter
{ l=tl;
b=tb;
}
void display()
{
System.out.println("\n");
System.out.println("Length of the rectangle = "+l);
System.out.println("Breadth of the rectangle = "+b);
}
public static void main(String arg[])
{
test4 r1=new test4();
r1.display();
test4 r2=new test4(6);
r2.display();
test4 r3=new test4(10,20);
r3.display();
}
}Java
Java - Constructor Demo

Written by
View ProfileSenthilvelan Sambamoorthy
Senthilvelan Sambamoorthy is working as a Professor in Department of Master of Computer Applications, Paavai Engineering College, Namakkal, Tamilnadu, India, He is Masters in Computers Applications. Having 18+ years of experience and his area of interest is Java, JSP, Servlet, J2EE, C#, PHP, Python ASP.NET, SQL Server and etc.
