A java constructor has the same name as the name of the class to which it belongs. Constructor's syntax does not include a return type, since constructors never return a value. Constructors may include parameters of various types. When the constructor is invoked using the new operator, the types must match those that are specified in the constructor definition. Java provides a default constructor which takes no arguments and performs no special actions or initializations, when no explicit constructors are provided. The only action taken by the implicit default constructor is to call the superclass constructor using the super() call. Constructor arguments provide you with a way to provide parameters for the initialization of an object.
Example:
public class MainClass {
double radius;
MainClass() {
}
MainClass(double theRadius) {
radius = theRadius;
}
}
Example:
Multiple Constructors
class Sphere {
int radius = 0;
Sphere() {
radius = 1;
}
Sphere(int radius) {
this.radius = radius;
}
}
Java Multiple Constructors
We can always use the default constructor of a class if it meets our needs. It is automatically there when a class is declared. Here are some examples to recap:
Circle redCircle = new Circle();
Circle blueCircle = new Circle();
Employee mechanic = new Employee();
Employee welder = new Employee();
As we saw last lesson, we can overwrite our default constructor with our own:
public class Circle{
int xcenter;
int ycenter;
int radius;
// our overwritten constructor
public Circle(){
radius = 1;
}
}
When the above constructor is used the radius has a value of 1. If we wanted to make a Constructor that allowed us to give our own value to its radius , for example , we could supply a constructor as follows:
public Circle(int _radius){
radius = _radius;
}
And it would be called like:
Circle smallCircle = new Circle(2); // radius = 2
or
Circle bigCircle = new Circle(569); // radius = 569
We could go even further and allow the users of our class to set every variable of our Circle class by supplying a further Constructor with 3 arguments :
public Circle(int _x, int _y, int _radius){
xcenter = _x;
ycenter = _y ;
radius = _radius;
}
Let us see what our class looks like with our added constructor now :
public class Circle{
int xcenter;
int ycenter;
int radius;
// our overwritten default constructor
public Circle(){
radius = 1;
}
// Constructor with 1 argument
public Circle(int _radius){
radius = _radius;
}
// our constructor with 3 arguments
public Circle(int _x, int _y, int _radius){
xcenter = _x;
ycenter = _y ;
radius = _radius;
}
}
So now we have 3 choices when we instantiate our Circle class. We can go :
Circle defaultCircle = new Circle(); // radius is 1
or :
Circle uniCircle = new Circle(27); // radius is 27
or :
Circle customCircle = new Circle(100, 75, 14);
This custom Circle will be created at position 100:75 , and have a radius of 14.
Of course, this Circle class would need set and get access methods to be written, especially if you wanted to move them around or change them someway.