public class Matango {
int hp;
//int level = 10; //field
final int LEVEL = 10; //Constant field
}
A constant field that prevents the value from being rewritten
Name with final
touppercase
(LEVEL)
public class Hero {
String name; //Name declaration
int hp; //HP Declaration
public void sleep() {
this.hp = 100;
System.out.println(this.name + "Sleeped and recovered");
}
It works even if this.
is omitted
If local variables and arguments have the same hp, they may take precedence.
Add this.
when using fields
public class Main {
public static void main(String[] args) {
//1. Generate a brave man
Hero h = new Hero();
}
}
Class name (Hero) Variable name (h) = new Class name (Hero) ();
public class Main {
public static void main(String[] args) {
//1. Generate a brave man
Hero h = new Hero();
//2. Set the initial value in the field
h.name = "Brave";
h.hp = 100;
System.out.println("Brave" + h.name + "Was born!");
}
}
Variable name. (H.) Field name (name) = Value ("Brave");
Recommended Posts