Java Constructors
Java Constructors
Java constructors are special methods that are called when an object is instantiated. A constructor initializes the created instance. Typically the constructor initializes the fields of the object that need initialization. Constructors can also take parameters, so fields can be initialized in the object at creation time.
Defining a Constructor
Here is an example of a class with a single constructor:
public class MyClass { public MyClass() { } }
It is this part that defines the constructor:
public MyClass() { }
First, an access modifier is written. These have the same meanings as for methods and fields. They determine what classes can access (call) the constructor.
Second, the name of the class is used as the name of the constructor method. This signals to the Java compiler that this is a constructor. Also notice that the constructor has no return type, like ordinary methods have.
Third, a list of parameters are declared inside the parentheses
()
. In the example above no parameters are declared. I will show an example of a constructor with parameters later in this text.
Fourth, the body of the constructor is defined inside the curly brackets
{ }
. In the example above the constructor has no operations inside the constructor body.Multiple Constructors
A class can have multiple constructors, as long as their signature (the parameters they take) are not the same. You can define as many constructors as you need.
Calling a Constructor
You call a constructor when you create a new instance of the class containing the constructor. Here is an example:
MyClass myClassVar = new MyClass();
This example invokes the no-argument constructor as defined earlier in this text.
In case you want to pass parameters to the constructor, you include the parameters between the parentheses after the class name, like this:
MyClass myClassVar = new MyClass("John", "Doe", 1975);
This example passes 3 parameters to the
MyClass
constructor. How to declare parameters in a constructor in the following section.Constructor Parameters
It is possible for a constructor to take parameters. These parameter can then be used to initialize the internal state of the newly created object. Here is an example:
public class Employee { private String firstName = null; private String lastName = null; private int birthYear = 0; public Employee(String first, String last, int year ) { firstName = first; lastName = last; birthYear = year; } }
In this example the constructor definition is marked in bold. As you can see, three parameters are declared:
first
, last
and year
. Inside the body of the constructor the values of these three parameters are assigned to the fields of the Employee
object.
The line breaks after each parameter are optional. The Java compiler ignores line breaks here. You can also write the parameter declaration in a single line if you want, like this:
public Employee(String first, String last, int year ) { firstName = first; lastName = last; birthYear = year; }
To this constructor that takes parameters, you would instantiate an
Employee
object like this:Employee employee = new Employee("Jack", "Daniels", 2000);
The parameters are passed to the constructor inside the parentheses after the class name on the right side of the equal sign. The object is then created, and the constructor executed. After execution of the above constructor, the fields initialized by the constructor will have the values of the parameters passed to the constructor.
Constructor Access Modifier
The access modifier of a constructor determines what classes in your application that are allowed to call that constructor. The access modifiers are explained in more detail in the text on Java access modifiers.
For instance, if a constructor is declared
protected
then only classes in the same package, or subclasses of that class can call that constructor.
A class can have multiple constructors, and each constructor can have its own access modifier. Thus, some constructors may be available to all classes in your application, while other constructors are only available to classes in the same package, subclasses, or even only to the class itself (private constructors).
Default, no-arg Constructor
You don't have to define a constructor for a class, but if you don't define any constructor, the Java compiler will insert a default, no-argument constructor. Thus, once the class is compiled it will always at least have a no-argument constructor.
If you do define a constructor for your class, then the Java compiler does not insert the default no-argument constructor into your class.
0 comments: