Java Constructors, Initialization, and Method Overloading Explained
This article explains how Java objects are initialized using explicit values, default values, and constructors, describes the characteristics and purpose of constructors, demonstrates their syntax with code examples, and introduces method overloading with practical illustrations.
Constructor Definition
In Java, a constructor is a special method whose name matches the class name and has no return value. It is used to initialize data members and can perform specific actions when an object is created.
Features of Constructors
Constructor name is identical to the class name.
Constructor does not have a return type.
Example of defining a Human class with a constructor that takes an integer height:
public class Test {
public static void main(String[] args) {
Human aPerson = new Human(160);
System.out.println(aPerson.getHeight());
}
}
class Human {
/** constructor */
Human(int h) {
this.height = h;
System.out.println("I'm born");
}
/** accessor */
int getHeight() {
return this.height;
}
int height;
}Running this program prints:
I'm born
160The constructor provides the initial value for height and executes the print statement as a side effect.
Initialization Priority
If a data member has an explicit initialization value, that value is used unless a constructor assigns a different value; the constructor’s assignment takes precedence over explicit and default values.
Example where an explicit value (170) is overridden by the constructor argument (160):
public class Test {
public static void main(String[] args) {
Human aPerson = new Human(160);
System.out.println(aPerson.getHeight());
}
}
class Human {
Human(int h) {
this.height = h;
}
int height = 170; // explicit initialization
int getHeight() { return this.height; }
}Output:
160Thus the order of precedence is: constructor assignment > explicit initialization > default initialization.
Method Overloading
A class can define multiple constructors or methods with the same name but different parameter lists. Java selects the appropriate one based on the arguments provided at the call site.
Example with two constructors:
class Human {
// constructor 1
Human(int h) {
this.height = h;
System.out.println("I'm born");
}
// constructor 2
Human(int h, String s) {
this.height = h;
System.out.println("Ne Zha: I'm born, " + s);
}
int getHeight() { return this.height; }
int height;
}
public class Test {
public static void main(String[] args) {
Human neZha = new Human(150, "shit");
System.out.println(neZha.getHeight());
}
}Output:
Ne Zha: I'm born, shit
150Similarly, method overloading is demonstrated with two breath methods—one without parameters and one with an int parameter—where the call aPerson.breath(10) invokes the second version.
class Human {
void breath() {
System.out.println("hu...hu...");
}
void breath(int rep) {
for (int i = 0; i < rep; i++) {
System.out.println("lu...lu...");
}
}
}
public class Test {
public static void main(String[] args) {
Human aPerson = new Human();
aPerson.breath(10);
}
}Output shows ten repetitions of "lu...lu..." confirming that the overloaded method with an integer argument is called.
Summary
Constructors share the class name and have no return type; they are used for initializing objects and can perform additional actions. Method overloading allows multiple methods (including constructors) with the same name but different parameter lists, and Java resolves calls based on the provided arguments.
Constructor features: same name as class, no return value.
Purpose: initialize data members and execute initial operations.
Method overloading: selection based on method name and parameter list.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.