Constructors are special methods invoked whenever an object of the class is created. Constructors are defined in the same fashion as defining methods. However, constructors must have the same name as the class name and there must not be any return types specified at the header of the constructors. Constructors are usually for initializing or setting instance variables in that class. How they are set is described in the body of the constructor together with other possible intructions.
Given a class called MyClass, its constructors are in the following structure.
public Myclass(<input argument list>){ // Body of the constructor }
Here is an example of a no-argument (no input) constructor for MyPoint, in which its instance variables are set with 1.0.
public MyPoint(){ x = 1.0; y = 1.0; }
Adding this constructor to the class definition of MyPoint, we obtain :
public class MyPoint { // data members private double x; private double y; // constuctors public MyPoint(){ x = 1.0; y = 1.0; } // …………………………………………… Details are omitted………………………………… public String toString(){ return "("+x+","+y+")"; } }
Once MyPoint is defined this way, whenever a MyPoint object is instantiated with new MyPoint(), a new object is created with x and y initialized with 1.0 due to that two statements in the constructor.
Constructors can be overloaded just like methods. A class can have multiple constructors with different input argument lists. Which constructor to be called when an instance of the class is created depends on the input argument list used with the new statement.
The no-argument constructor is the constructor that does not take any input arguments. Therefore, it usually contains a set of instructions that provide a default initialization to the object’s data members.
The detailed constructor usually refers to the constructor each input argument of which is corresponding to a data member of the class. Typical implementation of this constructor is to initialize every data member with its corresponding input argument.
The copy constructor usually refers to the constructor that takes another object of the same class as its input argument. It usually initializes each data member of the new object with the value of the corresponding data member of the input object. This results in that the new object has all of its attributes copied from the original one.
There can be other constructors apart from the three listed above. The rules of overloading constructors are the same as the ones governing the overloading of any other methods.
//Example Overloaded constructors Consider a new class definition of MyPoint listed below when overloaded constructors are added.
public class MyPoint { // data members private double x; private double y; // constructors public MyPoint(){ x = 1.0; y = 1.0; System.out.println("MyPoint() is called."); } public MyPoint(double x,double y){ this.x = x; this.y = y; System.out.println("MyPoint(double,double) is called."); } public MyPoint(MyPoint p){ x = p.getX(); y = p.getY(); System.out.println("MyPoint(MyPoint) is called."); } // …………………………………………… Details are omitted………………………………… public String toString(){ return "("+x+","+y+")"; } }
The first constructor, MyPoint(), does not take any input arguments. Therefore, it is called via the statement new Mypoint(). Such a constructor is the no-argument constructor. MyPoint(double x, double y) is a constructor that takes two double values as its input. It is called via the statement new Mypoint(a,b), where a and b are any double values.
This constructor initializes the instance variables to the input values. Such a constructor that requires the values of the instance variables as its input is the detailed constructor.
The last constructor is MyPoint(MyPoint q). This constructor is invoked as a response to the statement new Mypoint(c), where c is an instance of MyPoint.
In this constructor the value of x is set to the value of x from the instance of MyPoint supplied as the input to the constructor, and the value of y is set to the value of y from the same instance. Such a constructor that copies all attributes from the input instance is the copy constructor. Just like method overloading, you should notice that constructors are not limited to the ones shown in this example.
Also note that we add an invocation of println() inside each of the constructor to observe that which one of the constructors is invoked when each instance is created.
Observe the output of the following program by yourself. Pay attention to the order of constructors invoked through the messages printed out on the screen.
public class TestMyPoint5 { public static void main(String[] args) { MyPoint p = new MyPoint(); System.out.println("p-->"+p); MyPoint q = new MyPoint(2.0,5.0); System.out.println("q-->"+q); MyPoint r = new MyPoint(q); System.out.println("r-->"+r); } }
When there is no constructor provided in a class, it is still okay to create an instance of that class using the new statement without any input data members are not explicitly initialized when they are declared inside the class definition, default values will be used for those variables based on their data types (zero for numeric data type, false for boolean, and null for non-primitive types). Otherwise, the values explicitly used in the initialization are used.
However, if there is at least one constructor defined in the class other than the no-argument constructor which is absent, Java will treat the new statement without any input arguments in the same way as in the case of other missing overloaded constructors. That means it will produce a compilation error.
Consider the following example.
//Example Missing no-argument constructor
The following class definition does not contain the no-argument constructor but it does provide a detailed constructor. In the main() method, the program tries to create an instance of this class with the new statement without any input arguments on line 7.
The program cannot be compiled successfully since it cannot find the constructor that does not take any arguments. Java will not instantiate the object using the default mechanism due to the presence of a constructor (which is the detailed constructor, in this case) in the class definition. The error can be observed in following example output.
public class MissingConstructor { private double d; public MissingConstructor(double d){ this.d = d; } public static void main(String [] args){ MissingConstructor mc = new MissingConstructor(); } }
All Rights Reserved. © 2024 BookOfNetwork