- 객체가 생성될 때 객체를 초기화하는 특수한 메서드
- 객체의 초기화는 생성자를 통해서!
기본 생성자: 매개변수가 없는 생성자
생성자를 하나도 정의하지 않으면 자바 컴파일러는 기본 생성자를 자동으로 만든다
기본 생성자에서는 자동적으로 모든 멤버 변수들을 기본값으로 초기화한다 (필드가 int와 같은 수치형 변수라면 0으로, 참조형 변수라면 null로, 부울형 변수라면 false로 초기화한다)
기본 생성자가 추가되는 경우
package ex01;
class Box {
int width,depth,height;
}
public class BoxTest {
public static void main(String[] args) {
Box b = new Box();
System.out.println("상자의 크기: ("+b.width+" + "," + "+b.height+" + "," + "+b.depth+")");
} // 왜 빨간줄..?
}
기본 생성자가 추가되지 않는 경우
생성자를 하나라도 선언하면 컴파일러는 기본 생성자를 추가하지 않는다.
package ex01;
public class Box01 {
int width, height, depth;
public Box01(int w, int h, int d) {
width = w;
height = h;
depth = d;
}
public static void main(String[] args) {
Box01 b = new Box01(); // 오류발생
System.out.println(System.out.println("상자의 크기: ("+b.width+" + "," + "+b.height+" + "," + "+b.depth+")"););
}
}
Ex. Cat class
package ex04;
class Cat {
private String name;
private String color;
// 생성자 > 생성자도 stack이 있다
public Cat(String name, String color) { // this: heap변수
this.name = name;
this.color = color;
}
public String getName() {
return name;
}
public String getColor() {
return color;
}
}
public class CatTest {
public static void main(String[] args) {
Cat cat = new Cat("파랑", "흰색");
System.out.println(cat.getName());
System.out.println(cat.getColor());
}
}
C:\workspace\tools\jdk-21\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\lib\idea_rt.jar=52482:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\workspace\java_lec\study\out\production\study ex04.CatTest
파랑
흰색
Process finished with exit code 0
Ex. Burger class
package ex04;
class Burger {
private String name;
private int price;
// 생성자
public Burger(String n, int p) { //stack에 저장
name = n; // heap에 있는 데이터를 옮김
price = p;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
}
public class BurgerTest {
public static void main(String[] args) {
Burger b1 = new Burger("기본버거", 2000);
System.out.println(b1.getName());
System.out.println(b1.getPrice());
}
}
C:\workspace\tools\jdk-21\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\lib\idea_rt.jar=52475:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\workspace\java_lec\study\out\production\study ex04.BurgerTest
기본버거
2000
Process finished with exit code 0
Ex. Television class
package ex04;
class Television {
private int channel;
private int volume;
private boolean onOff;
// 생성자
Television(int c, int v, boolean o) {
channel = c;
volume = v;
onOff = o;
}
void print() {
System.out.println("채널은 " + channel + "이고 볼륨은 " + volume + "입니다.");
}
}
public class TelevisionTest01 {
public static void main(String[] args) {
Television myTv = new Television(7, 10, true);
myTv.print();
Television yourTv = new Television(9, 12, true);
yourTv.print();
}
}
C:\workspace\tools\jdk-21\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\lib\idea_rt.jar=52755:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\workspace\java_lec\study\out\production\study ex04.TelevisionTest01
채널은 7이고 볼륨은 10입니다.
채널은 9이고 볼륨은 12입니다.
Process finished with exit code 0
Share article