The constructor automatically passes information (fields) to the instance created by new.
public class Hero {
String name;
int hp;
}
Instantiating this Hero class will generate a Hero with no values (name is null and hp is 0).
public class Hero {
String name;
int hp;
//constructor
Hero(){
this.name = "name";
this.hp = 100;
}
}
This allows you to assign values to fields. But with this, you can only make Hero with the same name and the same HP. I can't make a wizard HP50 or a fairy HP70. So I'll do this so that I can create characters freely.
public class Hero {
String name;
int hp;
//constructor
Hero(String name , int hp){
this.name = name;
this.hp = hp;
}
}
What this does is that Hero (String name, int hp) takes name and hp and assigns them to the field. Where is it received from the main method?
public static void main(String[] args) {
Hero h = new Hero("Brave" , 100);
}
Since I set a constructor that receives two arguments in the Hero class, I set two arguments and call it when calling. If this is one argument, an error will occur.
The result is like this. Make sure the values are passed properly.
public static void main(String[] args) {
Hero h = new Hero("Brave" , 100);
System.out.println("Name is" + h.name + "is.");
System.out.println("HP is" + h.hp + "is.");
}
Result is
The name is brave.
HP is 100.
I was able to pass it properly.
But with this, if you want to pass only the name, you will get an error.
public class Main {
public static void main(String[] args) {
Hero h = new Hero("Brave");//Get an error
}
}
In such a case, add a constructor to the Hero method.
public class Hero {
String name;
int hp;
//constructor
Hero(String name , int hp){ //First constructor
this.name = name;
this.hp = hp;
}
Hero(String name){ //Second constructor
this.name = name;
this.hp = 50;
}
}
You can set multiple constructors with the same name but with different numbers of arguments. This is called overloading. If new is called in the main method and the arguments are name and hp, the first constructor will be called. If there is only name in the argument, the second constructor will be called. This will not cause an error, but if more fields are passed, it will be less correctable and less readable. So
this()
Is used. You can use it to call a constructor in the same method.
public class Hero {
String name;
int hp;
//constructor
Hero(String name , int hp){ //First constructor
this.name = name;
this.hp = hp;
}
Hero(String name){ //Second constructor
this(name , 50);
}
}
It will be like this. We are calling the first constructor in the second constructor. The first constructor has two arguments, so this () has two arguments in (). The name will be the one passed from the main method, and the hp will not be passed, so enter any hp. Here, it is set to 50. When you run it with this
public class Main {
public static void main(String[] args) {
Hero h = new Hero("Brave");
System.out.println("Name is" + h.name + "is.");
System.out.println("HP is" + h.hp + "is.");
}
}
result
The name is brave.
HP is 50.
You have been given 50 properly. This () is quite confusing. It's easier to understand if you write them one by one! You might think that, but then it will be difficult when you have to change the field later.
public class Hero {
String name;
int hp;
//constructor
Hero(String name , int hp){ //First constructor
this.name = name;
this.hp = hp;
}
Hero(String name){ //Second constructor
this.name = name;
this.hp = 50;
}
}
If you write like this and you have to change the field later
public class Hero {
String name1; //You have to change this to name1.
int hp;
//constructor
Hero(String name , int hp){ //First constructor
this.name = name; //An error will occur.
this.hp = hp;
}
Hero(String name){ //Second constructor
this.name = name; //An error will occur.
this.hp = 50;
}
}
In this case, you will have to fix two places. It's still good if there are two constructors, but as the number increases, the work becomes difficult. If you have fixed dozens of places, it is safe to make a mistake in one place.
It was the part I stumbled upon, so I summarized it. If you make a mistake, please point it out.
nkojima Thank you for pointing out. I deleted a part of the article. Certainly, when I read it back, it was different from readability. We will also refer to the article you sent.
Recommended Posts