Java Constructors
In the last topics you created an object with new and then set its fields one by one. That is long and easy to get wrong: forget one line and the object is half finished. A constructor is a special method that runs when an object is created and sets it up completely in one step.
Writing a constructor
A constructor has the same name as the class and no return type, not even void. Its parameters are given in the new expression:
class Student {
String name;
int grade;
Student(String n, int g) {
name = n;
grade = g;
}
public String toString() {
return name + " (grade " + grade + ")";
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("Ishaan", 8);
Student s2 = new Student("Mira", 10);
System.out.println(s1);
System.out.println(s2);
}
}Ishaan (grade 8) Mira (grade 10)
The toString method is the one from Class Methods.
The this keyword
It is natural to give the parameters the same names as the fields. But then the parameter shadows the field. The keyword this means "the current object", so this.name is the field and name is the parameter:
class Book {
String title;
double price;
Book(String title, double price) {
this.title = title;
this.price = price;
}
}
public class Main {
public static void main(String[] args) {
Book b = new Book("The Guide", 299.0);
System.out.println(b.title + " costs " + b.price);
}
}The Guide costs 299.0
This is the style you will see in almost all Java code.
Several constructors
Like methods, constructors can be overloaded. One constructor can call another with this(...) as its first statement, so the setup code is written only once:
class Pizza {
String size;
int toppings;
Pizza(String size, int toppings) {
this.size = size;
this.toppings = toppings;
}
Pizza(String size) {
this(size, 0);
}
Pizza() {
this("medium");
}
public String toString() {
return size + " pizza with " + toppings + " toppings";
}
}
public class Main {
public static void main(String[] args) {
System.out.println(new Pizza("large", 3));
System.out.println(new Pizza("small"));
System.out.println(new Pizza());
}
}large pizza with 3 toppings small pizza with 0 toppings medium pizza with 0 toppings
The default constructor and final fields
If a class has no constructor at all, Java adds an empty one without parameters. That is why new Dog() worked in Classes and Objects. As soon as you write any constructor, that free one is gone.
Constructors are also where final fields get their value, so each object can have its own value that never changes afterwards:
class Ticket {
static int next = 1001;
final int number;
final String passenger;
Ticket(String passenger) {
this.passenger = passenger;
this.number = next++;
}
}
public class Main {
public static void main(String[] args) {
Ticket t1 = new Ticket("Ravi");
Ticket t2 = new Ticket("Sana");
System.out.println(t1.number + " " + t1.passenger);
System.out.println(t2.number + " " + t2.passenger);
}
}1001 Ravi 1002 Sana
next++ uses the current value of the static counter and then increases it, so every ticket gets the next number.
Practice exercises
- Write a class
Carwith a constructor that takes the brand and the year, and print two cars. - Add a second constructor to
Carthat takes only the brand and uses the year 2025. - Give each
Cara final serial number taken from a static counter.