Constructor

생성자

  • Constructor : Initializing instance method(인스턴스 초기화 메소드!)

: Method that initializes instance(=iv group).

Time t = new Time();
t.hour = 12;
t.minute = 34;
t.second = 56;

Add constructor : To initialize instance(iv group) easily.

Time t = new Time(12,34,56);
class Car {
    String color;
    String gearType;
    int door;
    
    Car() {}//default constructor
    Car(String c, String g, ind d) {
        //iv group
        color = c;
        gearType = g;
        door = d;
    }
}
//1.declare reference type variable
//2.create object using operator new
//3.initialize with calling constructor
Car c = new Car("white","auto",4);
  1. constructor name = class name

  2. no return value.(no void)

  3. All class must have more than one constructor.

  • constructor type

  1. default constructor

no parameter

when there is no constructor, compiler adds automatically.

Last updated

Was this helpful?