Normal methods are designed to hold and execute a group of Java code, regardless of how they do it. Constructor methods, however, exist only to instantiate the class they are in.
In order to instantiate a class and create an object of that class, a call is made to that class's constructor method. For that reason, the constructor method must have the exact same name as its class, unlike typical methods. The call to instantiate a class usually looks something like:
MyClass = new MyClass();
Any methods within the class MyClass can now be invoked. The new keyword instantiates the class and the computer looks for the constructor method of that class to know what to do with this class. The method heading also has a few differences from a typical method:
public class MyClass {
public MyClass(){
}
}
The constructor method for MyClass only has the public access modifier, and in fact does not even need that. Unlike typical methods, the constructor method has no return type, not even void. Rather than compute and return a result, the constructor method returns an instance of the class itself, so it has no need for a return type as used by normal methods.
An important note to know is that constructor methods are not required in Java. If a class is written without a constructor method, a blank default constructor will be created by the Java Virtual Machine in the byte code of the program.
Parameters and Variables
As with other methods, any type of primitive value, variable or object can be passed into the constructor as a parameter. While a method cannot be passed into other methods, its object can be passed as a parameter and its methods invoked. Constructor methods can have none, one or many parameters, and require a special way to define variables in order to avoid name conflicts:
public class MyClass{
private int var1;
public MyClass(int var1){
this.var1 = var1;
}
}
The keyword this helps the JVM distinguish between the private variable in the beginning from the public variables in the constructor method.
Overloading
Just as a class can have a blank constructor method, it can also have multiple constructor methods. This process is known as overloading. The only required difference between the multiple constructors is in the parameters they accept. Say the program above might expect one or two parameters, but doesn't know precisely how many will be entered at a given time.
public class MyClass{
private int var1;
private int var2;
public MyClass(int var1){
this.var1 = var1;
}
public MyClass(int var1, int var2){
this.var1 = var1;
this.var2 = var2;
}
}
The second constructor method allows for a different situation in which two variables are passed as parameters rather than just one variable, as it is invoked below:
public class Example{
public static void main(String args[]){
MyClass x = new MyClass(1);
MyClass y = new MyClass(1,2);
}
}
Summary
Constructor methods are a very useful part of Java that many programmers take advantage of, particularly overloading methods. From their special variable requirements to their unique purpose in the Java program, constructor methods are complex and every Java programmer should research them thoroughly.
Join the Conversation