📋 생성자

객체를 생성하는 특별한 메서드

package section09;

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

    // 기본 생성자(default constructor)
    // public Person() {}

    public Person() {
        this.name = "아무개";
        this.age = 0;
    }

    // 매개변수가 있는 생성자
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void printInfo() {
        System.out.println("이름:\\t" + name + ", 나이:\\t" + age);
    }

}

package section09;

public class Ex0901 {

    public static void main(String[] args) {
        // 기본 생성자
        Person person1 = new Person();
        person1.printInfo();

        // 매개변수가 있는 생성자
        Person person2 = new Person("김일남", 99);
        person2.printInfo();
    }
}

✅ 생성자 오버로딩(Constructor Overloading)

객체 생성시 다양한 매개 변수 대응.

package section09;

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

    // 기본 생성자
    public Person() {
        this.name = "아무개";
        this.age = 0;
        this.address = "비공개";
    }

    // 이름만 매개변수로 받는 생성자
    public Person(String name) {
        this.name = name;
        this.age = 0;
        this.address = "비공개";
    }

    // 이름과 나이를 매개변수로 받는 생성자
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
        this.address = "비공개";
    }

    // 이름, 나이, 주소를 모두 매개변수로 받는 생성자
    public Person(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    // 이름, 나이, 주소를 출력하는 메서드
    public void printInfo() {
        System.out.println("이름:\\t" + name + ",\\t나이:\\t" + age + ",\\t주소:\\t" + address);
    }
}

package section09;

public class Ex0902 {
    public static void main(String[] args) {
        // 기본 생성자를 사용하여 객체 생성
        Person person1 = new Person();
        person1.printInfo();

        // 이름만 매개변수로 받는 생성자를 사용하여 객체 생성
        Person person2 = new Person("김일남");
        person2.printInfo();

        // 이름과 나이를 매개변수로 받는 생성자를 사용하여 객체 생성
        Person person3 = new Person("김이남", 98);
        person3.printInfo();

        // 이름, 나이, 주소를 모두 매개변수로 받는 생성자를 사용하여 객체 생성
        Person person4 = new Person("김삼남", 97, "부산진구 123");
        person4.printInfo();
    }
}

✅ 모호한 생성자 정의 주의!

package section09;

public class UnclearOverloading {
    String s;
    int a;
    int b;
    
    UnclearOverloading() {
        this.s = "java";
        this.a = 1;
        this.b = 2;
    }
    
    UnclearOverloading(String s, int a, int b) {
        this.s = s;
        this.a = a;
        this.b = b;
    }
    
    // 모호한 정의 주의!
    // UnclearOverloading(String s, int b, int a) {
    //     this.s = s;
    //     this.a = a;
    //     this.b = b;
    // }
    
    // b를 a보다 먼저 받고 싶다면 이렇게 할 수 있다.
    UnclearOverloading(int b, int a, String s) {
        this.s = s;
        this.a = a;
        this.b = b;
    }
}

package section09;

public class Ex0903 {
    public static void main(String[] args) {
        UnclearOverloading uo2 = new UnclearOverloading("JAVA", 10, 100);
        System.out.printf("%s, %d, %d", uo2.s, uo2.a, uo2.b);

        System.out.println();

        UnclearOverloading uo3 = new UnclearOverloading("JAVA", 100, 10);
        System.out.printf("%s, %d, %d", uo3.s, uo3.a, uo3.b);
        System.out.println();
    }
}

📋 값에 의한 호출, 객체에 의한 호출

✅ 값에 의한 호출 (Call by Value)

package section09.callbyvalue;

public class Ex0904 {
    public static void modifyValue(int value) {
        value = 20;
        System.out.println("modifyValue() 메서드 내 변경된 value:\\t" + value);
    }

    public static void main(String[] args) {
        int value = 10;
        System.out.println("modifyValue() 메서드 호출 전 value:\\t" + value);
        modifyValue(value); // 인수(value)의 복사본을 전달받아 사용하는 방식
        System.out.println("modifyValue() 메서드 호출 후 value:\\t" + value);
    }
}

/*
 * modifyValue() 메서드 호출 전 value: 10
 * modifyValue() 메서드 내 변경된 value: 20
 * modifyValue() 메서드 호출 후 value: 10
 */