English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Method overloading in Java programming

Similar to method overloading, constructor overloading is the process of creating and using constructors with different types of parameters. We can use this operator to refer to the constructor. Please see the following example.

Example

class A {
   public int a;
   public A() {
      this(-1);
   }
   public A(int a) {
      this.a = a;
   }
   public String toString() {
      return "[  a=  " + this.a + "]";
   }
}
public class Tester {
   public static void main(String args[]) {
      A a = new A(10);
      System.out.println(a);
      A a1 = new A();
   System.out.println(a1);
   }
}

Output Result

[  a= 10]
[  a= -1]