What is a Constructor
A constructor is a class method that creates a new object or instance of a class.
We’ve been using constructors ever since we introduced classes at Java Classes and Objects. For instance, in the block of code:
class Player {
String name = "Player";
int level = 1;
int exp = 0;
int hp = 20;
int mp = 5;
int monstersKilled = 0;
int timeElapsed = 0;
}
public class Main {
public static void main (String[] args) {
Player p1 = new Player();
}
}That function Player() we use to create a new object is a constructor of the class Player.
Creating a Constructor with No parameters
By default, a constructor will simply just create a new instance/object of the class.
However, we can do more than that.
For example, as we create the object, we can send a message that a player is online/connected.
So, to create a constructor, we create a method with the same name as the class itself. The constructor does not have a return type included.
class Player {
String name = "Player";
int level = 1;
int exp = 0;
int hp = 20;
int mp = 5;
int monstersKilled = 0;
int timeElapsed = 0;
Player () {
System.out.println(name + " is Online! Welcome back!");
}
}This way, if we create an object the usual way, the player will be greeted welcome.
public static void main (String[] args) {
Player p1 = new Player();
}Thus, the output will be:
Player is Online! Welcome back!
Creating a Constructor with parameters
We can also create a constructor with an argument.
For example, instead of the default name "Player", we can also create an argument to change the player name upon creating an object of the class.
class Player {
String name = "Player";
int level = 1;
int exp = 0;
int hp = 20;
int mp = 5;
int monstersKilled = 0;
int timeElapsed = 0;
Player () {
System.out.println(name + " is Online! Welcome back!");
}
Player (String chosenName) {
name = chosenName;
System.out.println(name + " is Online! Welcome back!");
}
}Therefore, if we create an object, we can specify the player name:
public static void main (String[] args) {
Player p1 = new Player("Martin");
}And the output will look like this:
Martin is Online! Welcome back
Constructor Overloading
Like normal methods, we can also use method overloading with constructors; that is, we can repeatedly define a constructor of the same class as song as each definition have different parameters.
We often make it to accommodate more possible inputs.
In this example, we created two definitions of the constructorPlayer, one with no parameters, and one with thechosenNameargument.This is because when an object is created, it makes it possible that the player is still greeted regardless if they set up their name or not. Therefore, handling two default values as well.
this keyword
On the topic of constructors, we can begin to use the keyword this.
This keyword is simply used to distinguish between parameters and attributes
One example is if we have a constructor of the Player class that greets them with their chosen name, such as the one below:
class Player {
String name = "Player";
// ...
Player (String name) {
this.name = name;
System.out.println(name + "is online! Hello again!");
}
}
public class Main {
public static void main (String[] args) {
Player p1 = new Player("Kim");
}
}The program would be able to distinguish between the parameter name and the class attribute name.
Therefore, without the keyword this, the class attribute will be referenced instead and the output will be:
Player is online! Hello again!
Instead of:
Kim is online! Hello again!
This keyword also applies when you have an class attribute and a method parameter with the same name:
class Point {
double x;
double y;
// ...
double distance (double x, double y) {
return Math.sqrt(Math.pow(x - this.x, 2) + Math.pow(y - this.y, 2));
}
}In the above example:
this.xandthis.yrepresents the class attributes,- while
xandyrepresent the parameters of the methoddistance()
In short, the this keyword refers to the current object in a method or a constructor, which allows us to differentiate between class attributes and parameters/variables of the same name.