GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||
GeezerVison is showing www.geeksforgeeks.org — Constructor Overloading in Java - GeeksforGeeks Constructor Overloading in JavaLast Updated : 21 Jan, 2026 Java supports constructor overloading, which allows a class to have more than one constructor with different parameter lists. The appropriate constructor is selected at compile time based on the arguments passed during object creation. Constructor overloading enables objects to be initialized in multiple ways, improving flexibility and code clarity. When do we need Constructor Overloading?Constructor overloading is useful when:
Example: The Thread class provides multiple constructors. For example:
If no arguments are required, the default constructor can be used. If a thread name is needed, a parameterized constructor is selected automatically. Problem Without Constructor OverloadingConsider a class Box with only one constructor:
In this case, the following statement is invalid:
The class does not support creating:
Example of Constructor OverloadingBy adding multiple constructors, different initialization options become available.
Output Volume of mybox1: 3000.0 Volume of mybox2: 0.0 Volume of mycube: 343.0 Using this() in Constructor OverloadingThe this() keyword is used to call one constructor from another constructor in the same class. It helps avoid code duplication and ensures consistent initialization.
Output 0.0 Rules for Using this() in Constructors
Invalid Example:
Note:
Constructor Overloading vs Method Overloading
Related TopicsComment Explore | |||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |