In Java, a constructor is a unique type of method that is used to initialize objects. A constructor is invoked when an object is created, and it has the same name as the class to which it belongs. The purpose of a constructor is to initialize the instance variables of an object and perform any necessary setup operations.
What Does a Java Constructor Do?
While syntactically similar to methods, constructors do not have a return type, and are used to set initial values to an object when an object is created.
How to Create a Constructor in Java
A simple constructor for a class called MyFirstConstructor is as follows:
class MyFirstConstructor{
int x;
MyFirstConstructor(){
x = 42;
}
}
When an object of the class MyFirstConstructor is created, the constructor of that class is the first thing to execute, setting the instance variable x to 42.
How to Invoke a Constructor
If you wanted to invoke a new instance of the MyFirstConstructor object from the example above, you call the constructor as you would any other method, and you must include the new keyword as well:
class Creator{
public static void main(String[] args){
// creates a new object of the class MyFirstConstructor
MyFirstConstructor objectOne = new MyFirstConstructor();
System.out.println("objectOne.x = " + objectOne.x);
}
}
/*
Output:
objectOne.x = 42
*/
Java Constructor with Parameters Example
Unlike the previous example, constructors often take in parameters. You can update the previous example by adding a parameter to the constructor.
class MySecondConstructor{
int x;
MySecondConstructor(int inputParam){
x = inputParam;
}
}
Now, the constructor takes an int as a parameter and sets x equal to the parameter. Now, when an object of MySecondConstructor is created, an int must be passed to the constructor.
Take a look at both examples in action:
class Creator{
public static void main(String[] args){
// creates a new object of the class MyFirstConstructor
MyFirstConstructor objectOne = new MyFirstConstructor();
// prints out objectOne's instance variable x
System.out.println("objectOne.x = " + objectOne.x);
//creates a new object of the class MySecondConstructor
MySecondConstructor objectTwo = new MySecondConstructor(10);
// prints out objectTwo's instance variable x
System.out.println("objectTwo.x = " + objectTwo.x);
}
}
/*
Output:
objectOne.x = 42
objectOne.x = 10
*/
Default Constructors
In Java, a default constructor is a constructor that is provided by the Java compiler if a class does not explicitly define any constructors. The default constructor is parameterless, meaning it takes no arguments. Its main purpose is to initialize the object's instance variables to default values or perform any other necessary setup operations. Here's an example of a class with a default constructor:
public class MyClass {
private int value;
// Default constructor provided by Java
// if no constructors are defined
public MyClass() {
// Initialization code for default constructor
// For example, setting default values
// for instance variables
this.value = 1;
}
}
Experiment with Java Constructors
In the code editor below, please demonstrate how to:
- Create a custom constructor in the Person class
- Use the new constructor to create two unique
Personobjects - Print the instance variables of each
Personobject
class Main {
public static void main(String[] args) {
// please write a custom constructor in
// the Person class as directed below
// (in the Person class)
// now use that new constructor to
// create two unique Person objects
// now print the instance var values
// of each object
}
}
class Person {
String name;
int age;
// create a fully qualified constructor
// (which is a constructor that takes in "name"
// and "age") and set the instance variables
// above from within the constructor
}
Summary: What is a Constructor in Java?
- A constructor is a method included in a class to create a new instance (aka "object") of that class
- A constructor does not have a return type
- The
newkeyword is used to create a new object - Constructors can contain parameters to instantiate an instance with specific values
- If you do not provide any Constructors, Java will provide a default constructor for you