[Java] Object-oriented syntax-Constructor

constructor

//Argument name in the constructor/Set age to the corresponding field
public class Person {
  public String name;
  public int age;

  //constructor
  public Person(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public String show() {
    return String.format("%s(%d).", this.name, this.age);
  }
}

ConstBasic.java


public class ConstBasic {
  public static void main(String[] args) {
    var p = new Person("Yamada Taro", 30);
    System.out.println(p.show()); //I'm Taro Yamada (30 years old).
  }
}

Default constructor

public class MyClass{
}
public class MyClass{
    public MyClass(){
    }
}

Overload

Person.java


public class Person {
  public String name;
  public int age;

  //constructor
  public Person(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public Person() {
    //Constructor call
    this("Hello Kitty", 46);
  }

  public void show() {
    System.out.printf("%s(%d).\n", this.name, this.age);
  }
}

ConstructorBasic.java


public class ConstructorBasic {

  public static void main(String[] args) {
    var p = new Person();
    p.show(); //Hello Kitty (46 years old).
  }
}

Initialization block

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;

public class Person {
  public String name;
  public int age;
  public LocalDateTime updated;

  //Initialization block
  {
    this.updated = LocalDateTime.now();
  }
  //First and last name,Initialize from birthday
  public Person(String firstName, String lastName, LocalDate birth) {
    this.name = lastName + " " + firstName;
    this.age = Period.between(birth, LocalDate.now()).getYears();
  }
  //name,Initialize from age
  public Person(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

Factory method

public class FactoryClass {
  //private constructor
  private FactoryClass() {
    System.out.println("Factory!!");
  }
  //Factory method
  public static FactoryClass getInstance() {
    return new FactoryClass();
  }
}

Recommended Posts

[Java] Object-oriented syntax-Constructor
[Java] Object-oriented
Object-oriented FizzBuzz (Java)
[Java] Object-oriented summary_Part 1
Object-oriented (Java) basics
[Java] Object-oriented summary_Part 2
[Java] Object-oriented syntax-Package
Object-oriented (java) with Strike Gundam
Object-oriented summary by beginners (Java)
Java
Java 3 major elements (object-oriented) memorandum
Java
[Java] Object-oriented syntax --class method / argument
[Java] Object-oriented syntax-class / field / method / scope
Summary of object-oriented programming using Java
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
java (override)
java (method)
Object-oriented summary
Java Day 2018
Java string
Object-oriented programming
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
[Java] Array
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
Java inheritance
[Java] Overload
Java basics