๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Today I Learned(TIL)/Java

Java ๊ธฐ๋ณธํŽธ ๊ฐ•์˜_์ƒ์„ฑ์ž

by carrot0911 2024. 11. 26.

์ƒ์„ฑ์ž

 

์ƒ์„ฑ์ž - ํ•„์š”ํ•œ ์ด์œ 

๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์‹œ์ ์— ์–ด๋–ค ์ž‘์—…์„ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ƒ์„ฑ์ž(Construct)๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

MemberInit

package construct;

public class MemberInit {
    String name;
    int age;
    int grade;
}

MemberInitMain1

package construct;

public class MemberInitMain1 {

    public static void main(String[] args) {
        MemberInit member1 = new MemberInit();
        member1.name = "user1";
        member1.age = 15;
        member1.grade = 90;

        MemberInit member2 = new MemberInit();
        member2.name = "user2";
        member2.age = 16;
        member2.grade = 80;

        MemberInit[] members = {member1, member2};

        for (MemberInit s : members) {
            System.out.println("์ด๋ฆ„: " + s.name + ", ๋‚˜์ด: " + s.age + ", ์„ฑ์ : " + s.grade);
        }
    }
}

ํšŒ์› ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋‚˜๋ฉด name, age, grade ๊ฐ™์€ ๋ณ€์ˆ˜์— ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค.
ํ•˜์ง€๋งŒ ํšŒ์›์˜ ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„์ด ๊ณ„์† ๋ฐ˜๋ณต๋˜๊ณ  ์žˆ๋‹ค.

package construct;

public class MemberInitMain2 {

    public static void main(String[] args) {
        MemberInit member1 = new MemberInit();
        initMember(member1, "user1", 15, 90);

        MemberInit member2 = new MemberInit();
        initMember(member2,"user2", 16, 80);

        MemberInit[] members = {member1, member2};

        for (MemberInit s : members) {
            System.out.println("์ด๋ฆ„: " + s.name + ", ๋‚˜์ด: " + s.age + ", ์„ฑ์ : " + s.grade);
        }
    }

    static void initMember (MemberInit member, String name, int age, int grade) {
        member.name = name;
        member.age = age;
        member.grade = grade;
    }
}

initMember(...) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐ˜๋ณต์„ ์ œ๊ฑฐํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด ๋ฉ”์„œ๋“œ๋Š” ๋Œ€๋ถ€๋ถ„ Member ๊ฐ์ฒด์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
์ด๋Ÿฐ ๊ฒฝ์šฐ ์†์„ฑ๊ณผ ๊ธฐ๋Šฅ์„ ํ•œ ๊ณณ์— ๋‘๋Š” ๊ฒƒ์ด ๋” ๋‚˜์€ ๋ฐฉ๋ฒ•์ด๋‹ค. ์‰ฝ๊ฒŒ ์ด์•ผ๊ธฐํ•ด์„œ MemberInit์ด ์ž๊ธฐ ์ž์‹ ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ธฐ๋Šฅ(๋ฉ”์„œ๋“œ)์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

 

this

MemberInit - initMember() ์ถ”๊ฐ€

package construct;

public class MemberInit {
    String name;
    int age;
    int grade;
    
    void initMember (String name, int age, int grade) {
        this.name = name;
        this.age = age;
        this.grade = grade;
    }
}
package construct;

public class MemberInitMain2 {

    public static void main(String[] args) {
        MemberInit member1 = new MemberInit();
        member1.initMember("user1", 15, 90);

        MemberInit member2 = new MemberInit();
        member2.initMember("user2", 16, 80);

        MemberInit[] members = {member1, member2};

        for (MemberInit s : members) {
            System.out.println("์ด๋ฆ„: " + s.name + ", ๋‚˜์ด: " + s.age + ", ์„ฑ์ : " + s.grade);
        }
    }
}

this

๋ฉค๋ฒ„ ๋ณ€์ˆ˜์™€ ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ๋‘˜์„ ์–ด๋–ป๊ฒŒ ๊ตฌ๋ถ„ํ•ด์•ผ ํ• ๊นŒ?

  • ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ณด๋‹ค ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์ฝ”๋“œ ๋ธ”๋ก์˜ ๋” ์•ˆ์ชฝ์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง„๋‹ค.
    ๋”ฐ๋ผ์„œ initMember(String name, ...) ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ name์ด๋ผ๊ณ  ์ ์œผ๋ฉด ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•˜๊ฒŒ ๋œ๋‹ค.
  • ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•˜๋ ค๋ฉด ์•ž์— this.์ด๋ผ๊ณ  ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. this๋Š” ์ธ์Šคํ„ด์Šค ์ž์‹ ์˜ ์ฐธ์กฐ๊ฐ’์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

this์˜ ์ƒ๋žต

this๋Š” ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ณ€์ˆ˜๋ฅผ ์ฐพ์„ ๋•Œ ๊ฐ€๊นŒ์šด ์ง€์—ญ๋ณ€์ˆ˜(๋งค๊ฐœ๋ณ€์ˆ˜๋„ ์ง€์—ญ๋ณ€์ˆ˜)๋ฅผ ๋จผ์ € ์ฐพ๊ณ  ์—†์œผ๋ฉด ๊ทธ๋‹ค์Œ์œผ๋กœ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค. ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋„ ์—†์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

package construct;

public class MemberThis {
    String nameField;

    void initMember (String nameParameter) {
        nameField = nameParameter;
    }
}
  • nameField๋Š” ๋จผ์ € ์ง€์—ญ๋ณ€์ˆ˜(๋งค๊ฐœ๋ณ€์ˆ˜)์—์„œ ๊ฐ™์€ ์ด๋ฆ„์ด ์žˆ๋Š”์ง€ ์ฐพ๋Š”๋‹ค. ์ด ๊ฒฝ์šฐ ์—†์œผ๋ฏ€๋กœ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์—์„œ ์ฐพ๋Š”๋‹ค.
  • nameParameter๋Š” ์ง€์—ญ๋ณ€์ˆ˜(๋งค๊ฐœ๋ณ€์ˆ˜)์—์„œ ๊ฐ™์€ ์ด๋ฆ„์ด ์žˆ๋Š”์ง€ ์ฐพ๋Š”๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

this์™€ ์ฝ”๋”ฉ ์Šคํƒ€์ผ

package construct;

public class MemberThis {
    String nameField;

    void initMember (String nameParameter) {
        this.nameField = nameParameter;
    }
}

this.nameField๋ฅผ ๋ณด๋ฉด this๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ƒ๋žตํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.
์ด๋ ‡๊ฒŒ this๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด ์ฝ”๋“œ๊ฐ€ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ˆˆ์œผ๋กœ ์‰ฝ๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
์‰ฝ๊ฒŒ ์ด์•ผ๊ธฐํ•ด์„œ this๋ฅผ ๊ฐ•์ œ๋กœ ์‚ฌ์šฉํ•ด์„œ, ์ง€์—ญ ๋ณ€์ˆ˜(๋งค๊ฐœ๋ณ€์ˆ˜)์™€ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ๋ˆˆ์— ๋ณด์ด๋„๋ก ๊ตฌ๋ถ„ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

์ƒ์„ฑ์ž - ๋„์ž…

๋Œ€๋ถ€๋ถ„์˜ ๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ž๋งˆ์ž ์ฆ‰์‹œ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์„ ์ข€ ๋” ํŽธ๋ฆฌํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ƒ์„ฑ์ž๋ผ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์‹œ์ ์—์„œ ์ฆ‰์‹œ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

MemberConstruct

package construct;

public class MemberConstruct {
    String name;
    int age;
    int grade;

    MemberConstruct (String name, int age, int grade) {
        System.out.println("์ƒ์„ฑ์ž ํ˜ธ์ถœ name: " + name + ", age: " + age + ", grade: " + grade);
        this.name = name;
        this.age = age;
        this.grade = grade;
    }
}

์ƒ์„ฑ์ž๋Š” ๋ฉ”์„œ๋“œ์™€ ๋น„์Šทํ•˜์ง€๋งŒ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

  • ์ƒ์„ฑ์ž์˜ ์ด๋ฆ„์€ ํด๋ž˜์Šค ์ด๋ฆ„๊ณผ ๊ฐ™์•„์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ฒซ ๊ธ€์ž๋„ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•œ๋‹ค.
  • ์ƒ์„ฑ์ž๋Š” ๋ฐ˜ํ™˜ ํƒ€์ž…์ด ์—†๋‹ค. ๋น„์›Œ๋‘์–ด์•ผ ํ•œ๋‹ค.
  • ๋‚˜๋จธ์ง€๋Š” ๋ฉ”์„œ๋“œ์™€ ๊ฐ™๋‹ค.

ConstructMain1

package construct;

public class ConstructMain1 {

    public static void main(String[] args) {
        MemberConstruct member1 = new MemberConstruct("user1", 15, 90);
        MemberConstruct member2 = new MemberConstruct("user2", 16, 80);

        MemberConstruct[] members = {member1, member2};

        for (MemberConstruct s : members) {
            System.out.println("์ด๋ฆ„: " + s.name + ", ๋‚˜์ด: " + s.age + ", ์„ฑ์ : " + s.grade);
        }
    }
}

์ƒ์„ฑ์ž ํ˜ธ์ถœ

์ƒ์„ฑ์ž๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋‚˜์„œ ์ฆ‰์‹œ ํ˜ธ์ถœ๋œ๋‹ค.

new ์ƒ์„ฑ์ž์ด๋ฆ„(์ƒ์„ฑ์ž์— ๋งž๋Š” ์ธ์ˆ˜ ๋ชฉ๋ก)
new ํด๋ž˜์Šค์ด๋ฆ„(์ƒ์„ฑ์ž์— ๋งž๋Š” ์ธ์ˆ˜ ๋ชฉ๋ก)

new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๋งˆ์ง€๋ง‰์— ๊ด„ํ˜ธ( )๋„ ํฌํ•จํ•ด์•ผ ํ•˜๋Š” ์ด์œ ๊ฐ€ ๋ฐ”๋กœ ์ƒ์„ฑ์ž ๋•Œ๋ฌธ์ด๋‹ค.
๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ฉด์„œ ๋™์‹œ์— ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ํฌํ•จํ•œ๋‹ค.

์ƒ์„ฑ์ž ์žฅ์ 

์ค‘๋ณต ํ˜ธ์ถœ ์ œ๊ฑฐ

  • ์ƒ์„ฑ์ž ๋•๋ถ„์— ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ฉด์„œ ๋™๊ธฐ์— ์ƒ์„ฑ ์งํ›„์— ํ•„์š”ํ•œ ์ž‘์—…์„ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

์ œ์•ฝ - ์ƒ์„ฑ์ž ํ˜ธ์ถœ ํ•„์ˆ˜

  • ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ง์ ‘ ์ •์˜ํ•œ ์ƒ์„ฑ์ž๊ฐ€ ์žˆ๋‹ค๋ฉด ์ง์ ‘ ์ •์˜ํ•œ ์ƒ์„ฑ์ž๋ฅผ ๋ฐ˜๋“œ์‹œ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค.

์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•„์ˆ˜ ๊ฐ’ ์ž…๋ ฅ์„ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

 

โœ”๏ธ ์ฐธ๊ณ 

์ข‹์€ ํ”„๋กœ๊ทธ๋žจ์€ ๋ฌดํ•œํ•œ ์ž์œ ๋„๊ฐ€ ์ฃผ์–ด์ง€๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์•„๋‹ˆ๋ผ ์ ์ ˆํ•œ ์ œ์•ฝ์ด ์žˆ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค.

 

๊ธฐ๋ณธ ์ƒ์„ฑ์ž

๊ธฐ๋ณธ ์ƒ์„ฑ์ž

  • ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ์ƒ์„ฑ์ž๋ฅผ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ผ๊ณ  ํ•œ๋‹ค.
  • ํด๋ž˜์Šค์— ์ƒ์„ฑ์ž๊ฐ€ ํ•˜๋‚˜๋„ ์—†์œผ๋ฉด ์ž๋ฐ” ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๊ณ , ์ž‘๋™ํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ์—†๋Š” ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.
  • ์ƒ์„ฑ์ž๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ์žˆ์œผ๋ฉด ์ž๋ฐ”๋Š” ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๋Š”๋‹ค.

MemberDefault

package construct;

public class MemberDefault {
    String name;

    MemberDefault () {

    }
}

๊ธฐ๋ณธ์ƒ์„ฑ์ž๋ฅผ ์™œ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ค„๊นŒ?

์ž๋ฐ”์—์„œ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ƒ์„ฑ์ž ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋„ ๋ชจ๋“  ํด๋ž˜์Šค์— ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค.
์ƒ์„ฑ์ž ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ํŽธ์˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

 

์ƒ์„ฑ์ž - ์˜ค๋ฒ„๋กœ๋”ฉ๊ณผ this( )

์ƒ์„ฑ์ž๋„ ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋กœ๋”ฉ์ฒ˜๋Ÿผ ๋งค๊ฐœ๋ณ€์ˆ˜๋งŒ ๋‹ค๋ฅด๊ฒŒ ํ•ด์„œ ์—ฌ๋Ÿฌ ์ƒ์„ฑ์ž๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค.

MemberConstruct - ์ƒ์„ฑ์ž ์ถ”๊ฐ€

package construct;

public class MemberConstruct {
    String name;
    int age;
    int grade;

    MemberConstruct (String name, int age) {
        this.name = name;
        this.age = age;
        this.grade = 50;
    }

    MemberConstruct (String name, int age, int grade) {
        System.out.println("์ƒ์„ฑ์ž ํ˜ธ์ถœ name: " + name + ", age: " + age + ", grade: " + grade);
        this.name = name;
        this.age = age;
        this.grade = grade;
    }
}

๊ธฐ์กด MemberConstruct์— ์ƒ์„ฑ์ž๋ฅผ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•ด์„œ ์ƒ์„ฑ์ž๊ฐ€ 2๊ฐœ๊ฐ€ ๋˜์—ˆ๋‹ค.

MemberConstruct(String name, int age)
MemberConstruct(String name, int age, int grade)
package construct;

public class ConstructMain2 {

    public static void main(String[] args) {
        MemberConstruct member1 = new MemberConstruct("user1", 15, 90);
        MemberConstruct member2 = new MemberConstruct("user2", 16);

        MemberConstruct[] members = {member1, member2};

        for (MemberConstruct s : members) {
            System.out.println("์ด๋ฆ„: " + s.name + ", ๋‚˜์ด: " + s.age + ", ์„ฑ์ : " + s.grade);
        }
    }
}

์ƒ์„ฑ์ž๋ฅผ ์˜ค๋ฒ„๋กœ๋”ฉํ•œ ๋•๋ถ„์— ์„ฑ์  ์ž…๋ ฅ์ด ๊ผญ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” grade๊ฐ€ ์žˆ๋Š” ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” grade๊ฐ€ ์—†๋Š” ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค. grade๊ฐ€ ์—†๋Š” ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์„ฑ์ ์€ 50์ ์ด ๋œ๋‹ค.

์ด๋•Œ this( )๋ผ๋Š” ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ƒ์„ฑ์ž ๋‚ด๋ถ€์—์„œ ์ž์‹ ์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.
์ฐธ๊ณ ๋กœ this๋Š” ์ธ์Šคํ„ด์Šค ์ž์‹ ์˜ ์ฐธ์กฐ๊ฐ’์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ๊ทธ๋ž˜์„œ ์ž์‹ ์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

MemberConstruct - this( ) ์‚ฌ์šฉ

package construct;

public class MemberConstruct {
    String name;
    int age;
    int grade;

    MemberConstruct (String name, int age) {
    	this(name, age, 50);
    }

    MemberConstruct (String name, int age, int grade) {
        System.out.println("์ƒ์„ฑ์ž ํ˜ธ์ถœ name: " + name + ", age: " + age + ", grade: " + grade);
        this.name = name;
        this.age = age;
        this.grade = grade;
    }
}

this( ) ๊ทœ์น™

  • this( )๋Š” ์ƒ์„ฑ์ž ์ฝ”๋“œ์˜ ์ฒซ ์ค„์—๋งŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.