java (constructor)

How to define a construct

① The method name is exactly the same as the class name (2) The return value is not described in the method declaration (void is also useless)

Hero class that defines the behavior immediately after birth

public class Hero {
  String name;
  int hp;
  public Hero() {   //Definition of contrast
    this.hp = 100;  //Initialize hp field with 100
  }
}

Method that wrote the process that is automatically executed immediately after being new

Cost Lacta creates a defined Hero

public class Main {
  public static void main(String[] args) {
    Hero h = new Hero();      //100 is assigned to HP at the same time as instant generation
    h.hp = 100;               //I do not need

    System.out.println(h.hp); //Is displayed as 100
  }
}

Receive arguments as additional information in the constructor

public class Hero {
  String name;
  int hp;
  public Hero(String name) {   //Receives one string as an argument
    this.hp = 100;             //Initialize hp field with 100
    this.name = name;          //Initialize the name field with the value of the argument
  }
}

pass arguments with new

public class Main {
  public static void main(String[] args) {
    Hero h = new Hero("Brave");      //After the instant is generated, the JVM automatically executes the constructor as an argument."Brave"Is used

    System.out.println(h.hp);   //Is displayed as 100
    System.out.println(h.name); //Displayed as hero
  }
}

Constructor overload

public class Hero {
  String name;
  int hp;
  public Hero(String name) {   //Constrasta ①
    this.name = name;          
  }
  public Hero() {              //Constructor ②
    this.name = "Brave 2"        //"Brave 2"The set
  }
}

Use of classes overloaded with constasters

public class Main {
  public static void main(String[] args) {
    Hero h = new Hero("Brave");      //Since there is a string argument, the constructor ① is called.
    System.out.println(h.name);    //Displayed as hero

    Hero h2 = new Hero();          //Constructor ② is called because there are no arguments
    System.out.println(h2.name);   //Displayed as Hero 2
  }
}

Recommended Posts

java (constructor)
Java constructor
Understand java constructor
Studying Java 8 (see constructor)
Java
JAVA constructor call processing
Java
constructor
I studied the constructor (java)
Java learning (0)
[Java] array
Java protected
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
[For beginners] Summary of java constructor
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
[Scala] Constructor
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java
[Java] Annotation
java notes