Constructor
public class ConstructorExample {
//Constructor is a special type of method that is used to initialize the object.
public ConstructorExample() {
System.out.println("Inside defaut constructor");
}
public ConstructorExample(int a) {
System.out.println("Inside parameterized constructor accepting argument:"+a);
}
public ConstructorExample(int a,String b) {
System.out.println("Inside parameterized constructor accepting arguments:"+a+","+b);
}
public static void main(String[] args) {
ConstructorExample ce1=new ConstructorExample(); // calls default constructor
ConstructorExample ce2=new ConstructorExample(100); // calls parameterized constructor
ConstructorExample ce3=new ConstructorExample(100,"hai");
}
}
Output:
Inside defaut constructor
Inside parameterized constructor accepting argument:100
Inside parameterized constructor accepting arguments:100,hai