Sub topic 5
Initializing objects with constructors
A constructor is a special part of a Java class that sets up a new object. A class is a definition for a kind of object, and an object is one instance created from that definition.
A constructor gives an object its starting state. For example, a plant object might need a name and a starting height as soon as it is created.
Write and call a constructor
A field is a variable that belongs to an object. The Plant class below has two fields. Its constructor gives both fields values.
Save this program as Main.java:
class Plant {
String name;
int heightCm;
Plant(String name, int heightCm) {
this.name = name;
this.heightCm = heightCm;
}
void describe() {
System.out.println(name + ": " + heightCm + " cm");
}
}
public class Main {
public static void main(String[] args) {
Plant first = new Plant("Fern", 12);
Plant second = new Plant("Basil", 8);
first.describe();
second.describe();
}
}Compile and run it:
javac Main.java
java MainThe output is:
Fern: 12 cm
Basil: 8 cmEach plant has its own field values. The constructor sets those values when each object is created.
How the constructor works
Look at the constructor header:
Plant(String name, int heightCm) {The constructor has the same name as the class. It has no return type, not even void. The words inside the parentheses declare its parameters. A parameter is a variable that receives a value when the constructor is called.
Now look at the creation of the first plant:
Plant first = new Plant("Fern", 12);The new keyword creates an object and calls its constructor. The values "Fern" and 12 are arguments. An argument is a value passed to a parameter. Here, "Fern" goes to the name parameter, and 12 goes to the heightCm parameter.
After the constructor finishes, first refers to the new object. The second line creates a separate object and calls the same constructor with different arguments.
Using `this`
Inside the constructor, this refers to the object being created. It helps distinguish a field from a parameter with the same name:
this.name = name;The left side, this.name, is the object’s field. The right side, name, is the parameter. The statement copies the parameter’s value into the field. The next statement does the same for heightCm.
Constructors and methods
A method is a named action that can be called on an object. In the example, describe is a method. It prints the plant’s current fields.
A constructor is different: Java calls it as part of creating an object with new. You do not call it later with first.Plant(...).
When you write no constructor
For a normal class with no declared constructors, Java supplies a no-argument constructor. A no-argument constructor takes no values inside its parentheses.
For example, save this as SeedDemo.java:
class Seed {
int count;
}
public class SeedDemo {
public static void main(String[] args) {
Seed seed = new Seed();
System.out.println(seed.count);
}
}Run it with:
javac SeedDemo.java
java SeedDemoIt prints 0. An int field that has not been given another value starts at 0.
Java supplies that no-argument constructor because Seed declares no constructors. By contrast, Plant declares a constructor that needs a name and height. Java does not also supply a no-argument constructor for it, so new Plant() would not compile.
Exercise
What values will the fields have after Plant herb = new Plant("Mint", 5);?
Show answer
The name field will be "Mint", and the heightCm field will be 5.
Exercise
Why does new Plant() fail for the Plant class in this chapter?
Show answer
Plant declares a constructor that requires a String and an int. Java does not supply a no-argument constructor when the class already declares a constructor.