CONSTRUCTOR OVERLOADING

Overloading a constructor means typing in multiple versions of the constructor, each having a different argument list, like the following examples:

1
2
3
4
5
6
7
8
9
class Car
{
Car()
{
}
Car(String s)
{
}
}

The preceding Car class has two overloaded constructors, one that takes a string, and one with no arguments. Because there's no code in the no-arg version, it's actually identical to the default constructor the compiler supplies, but remember-since there's already a constructor in this class (the one that takes a string), the compiler won't supply a default constructor. If you want a no-arg constructor to overload the with-args version you already have, you're going to have to type it yourself, just as in the Car example.

Overloading a constructor is typically used to provide alternate ways for clients to instantiate objects of your class. For example, if a client knows the Car name, they can pass that to a Car constructor that takes a string. But if they don't know the name, the client can call the no-arg constructor and that constructor can supply a default name.

Important points related to Constructor overloading:
Constructor overloading is similar to method overloading in Java.
You can call overloaded constructor by using this() keyword in Java.
Overloaded constructor must be called from another constructor only.
Make sure you add no argument default constructor because once compiler will not add if you have added any constructor in Java.
If an overloaded constructor called, it must be first statement of constructor in java.
It's best practice to have one primary constructor and let overloaded constructor calls that. this way your initialization code will be centralized and easier to test and maintain.
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class ConstructorWithIIB1
    {
        ConstructorWithIIB1(int i)
            {
                System.out.println("constructor");
            }
        {
            System.out.println("IIB");
        }
    public static void main(String[] args)
        {
            ConstructorWithIIB1 g1 = new ConstructorWithIIB1(10);
            System.out.println("---------");
            ConstructorWithIIB1 g2 = new ConstructorWithIIB1(20);
            System.out.println("---------");
            ConstructorWithIIB1 g3 = new ConstructorWithIIB1(30);
            System.out.println("---------");
    }
}

O/P:

ConstructorWithIIB1

NOTE:

Both IIB and Constructor are only called when you create your object.
IIB called before the constructor when you create your object.
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

21

22
class ConstructorWithIIB2
    {
        {
            System.out.println("IIB1");      
        }
    ConstructorWithIIB2()
        {
            System.out.println("constructor");
        }
        {
            System.out.println("IIB2");  
        }
    public static void main(String[] args)
        {
            ConstructorWithIIB2 i1 = new ConstructorWithIIB2();
            System.out.println("---------");
            ConstructorWithIIB2 i2 = new ConstructorWithIIB2();
            System.out.println("---------");
            ConstructorWithIIB2 i3 = new ConstructorWithIIB2();
            System.out.println("---------");
        }
}

 OP:

ConstructorWithIIB1