Java๐ŸŽ€

๐Ÿ’™์ƒ์„ฑ์ž, this์™€ this(), ๊ทธ๋ฆฌ๊ณ  ๋‚ด๋ถ€ํด๋ž˜์Šค

Jeein0313 2023. 2. 24. 11:35

1๏ธโƒฃ์ƒ์„ฑ์ž๋ž€?

 

์ƒ์„ฑ์ž๋Š” ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค. ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ๋กœ, new ์—ฐ์‚ฐ์ž๊ฐ€ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋ฉด, ์ƒ์„ฑ์ž๋Š” ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋“ค(ํ•„๋“œ๊ฐ’๋“ค)์„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. (์ƒ์„ฑ์ž๋Š” ๋ฆฌํ„ด ํƒ€์ž…์ด ์—†๊ณ , ํด๋ž˜์Šค ์ด๋ฆ„๊ณผ ๊ฐ™์•„์•ผ ํ•œ๋‹ค.)

class Test{
    int a,b;
    public Test() {
        System.out.println("๊ธฐ๋ณธ ์ƒ์„ฑ์ž");
    }
    public Test(int a){
        System.out.println("๋งค๊ฐœ๋ณ€์ˆ˜ 1๊ฐœ ์žˆ๋Š” ์ƒ์„ฑ์ž");
    }
    public Test(int a, int b){
        System.out.println("๋งค๊ฐœ๋ณ€์ˆ˜ 2๊ฐœ ์žˆ๋Š” ์ƒ์„ฑ์ž");
    }
}


public class Constructor {
    public static void main(String[] args) {
        Test t1=new Test();
        Test t2=new Test(1);
        Test t3=new Test(1,2);

        /*
            ๊ธฐ๋ณธ ์ƒ์„ฑ์ž
            ๋งค๊ฐœ๋ณ€์ˆ˜ 1๊ฐœ ์žˆ๋Š” ์ƒ์„ฑ์ž
            ๋งค๊ฐœ๋ณ€์ˆ˜ 2๊ฐœ ์žˆ๋Š” ์ƒ์„ฑ์ž
         */
    }

}

 

์ฐธ๊ณ ๐Ÿ””)

ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•  ๋•Œ ์ƒ์„ฑ์ž๋ฅผ ์ƒ๋žตํ•˜๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™์œผ๋กœ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž(Default Constructor)๋ฅผ ์ƒ์„ฑํ•ด ์ค€๋‹ค. ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋Š” ํ•„๋“œ๊ฐ’์ด ์—†๋Š” ์ƒ์„ฑ์ž๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ์ด๋‹ค.

class A{
	A(){} //๊ธฐ๋ณธ ์ƒ์„ฑ์ž
}

ํ•˜์ง€๋งŒ, ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์€ ์ƒ์„ฑ์ž๊ฐ€ ์•„๋ฌด๊ฒƒ๋„ ์—†์„ ๋•Œ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋งŒ์•ฝ ์˜ค๋ฒ„๋กœ๋”ฉํ•œ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๊ฐ€ ํ•„์š”ํ•  ๊ฒฝ์šฐ, ์ƒ๋žตํ•  ์ง€ ๋ง๊ณ  ์จ์ค˜์•ผ ํ•œ๋‹ค.

 

 

 

2๏ธโƒฃthis vs this()

 

this() ๋ฉ”์„œ๋“œ๋Š” ์ž์‹ ์ด ์†ํ•œ ํด๋ž˜์Šค์—์„œ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค. this() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ ๋‘๊ฐ€์ง€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•ด์•ผ ํ•œ๋‹ค.

  • this() ๋ฉ”์„œ๋“œ๋Š” ๋ฐ˜๋“œ์‹œ ์ƒ์„ฑ์ž ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • this() ๋ฉ”์„œ๋“œ๋Š” ๋ฐ˜๋“œ์‹œ ์ƒ์„ฑ์ž์˜ ์ฒซ์ค„์— ์œ„์น˜ํ•ด์•ผ ํ•œ๋‹ค.
class Test{
    int a,b;
    public Test() {
        System.out.println("๊ธฐ๋ณธ ์ƒ์„ฑ์ž");
    }
    public Test(int a){
        this();
        System.out.println("๋งค๊ฐœ๋ณ€์ˆ˜ 1๊ฐœ ์žˆ๋Š” ์ƒ์„ฑ์ž");
    }
    public Test(int a, int b){
        this(a);
        System.out.println("๋งค๊ฐœ๋ณ€์ˆ˜ 2๊ฐœ ์žˆ๋Š” ์ƒ์„ฑ์ž");
    }
}


public class Constructor {
    public static void main(String[] args) {
        Test t1=new Test();
        Test t2=new Test(1);
        Test t3=new Test(1,2);

        /*
            ๊ธฐ๋ณธ ์ƒ์„ฑ์ž
            ๊ธฐ๋ณธ ์ƒ์„ฑ์ž
            ๋งค๊ฐœ๋ณ€์ˆ˜ 1๊ฐœ ์žˆ๋Š” ์ƒ์„ฑ์ž
            ๊ธฐ๋ณธ ์ƒ์„ฑ์ž
            ๋งค๊ฐœ๋ณ€์ˆ˜ 1๊ฐœ ์žˆ๋Š” ์ƒ์„ฑ์ž
            ๋งค๊ฐœ๋ณ€์ˆ˜ 2๊ฐœ ์žˆ๋Š” ์ƒ์„ฑ์ž
         */
    }

}

 

this ํ‚ค์›Œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ์„ฑ์ž์—์„œ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ํž˜๋“ค๋•Œ, ์ด๋ฅผ ๊ตฌ๋ถ„ํ•ด์ฃผ๊ธฐ ์œ„ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

class Student{
    String name;
    int age;
    String mbti;

    public Student(String name, int age, String mbti) {
        this.name = name;
        this.age = age;
        this.mbti = mbti;
    }
}

๋ชจ๋“  ๋ฉ”์„œ๋“œ์—๋Š” ์ž์‹ ์ด ํฌํ•จ๋œ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” this๋ผ๋Š” ์ฐธ์กฐ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ, ์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ this.๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋žตํ•œ๋‹ค. ์ฆ‰, this๋Š” ์ธ์Šคํ„ด์Šค ์ž์‹ ์„ ๊ฐ€๋ฆฌํ‚ค๋ฉฐ this๋ฅผ ํ†ตํ•ด ์ธ์Šคํ„ด์Šค ์ž์‹ ์˜ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

3๏ธโƒฃ๋‚ด๋ถ€ ํด๋ž˜์Šค

 

ํด๋ž˜์Šค ๋‚ด์— ์„ ์–ธ๋œ ํด๋ž˜์Šค๋กœ, ์™ธ๋ถ€ ํด๋ž˜์Šค์™€ ๋‚ด๋ถ€ ํด๋ž˜์Šค๊ฐ€ ๊ธด๋ฐ€ํžˆ ์—ฐ๊ด€๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค. ๋‚ด๋ถ€ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„๋“ค์— ์‰ฝ๊ฒŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๊ณ , ์ฝ”๋“œ์˜ ๋ณต์žก์„ฑ์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ, ์™ธ๋ถ€์ ์œผ๋กœ ๋ถˆํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ถœ ์ˆ˜ ์žˆ์–ด ์บก์Аํ™”๋ฅผ ๋‹ฌ์„ฑํ•˜๋Š” ๋ฐ ์œ ์šฉํ•˜๋‹ค.

 

๋‚ด๋ถ€ ํด๋ž˜์Šค์—๋Š” ์ธ์Šคํ„ด์Šค ๋‚ด๋ถ€ ํด๋ž˜์Šค, ์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค, ์ง€์—ญ ๋‚ด๋ถ€ ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค.

class Outer { // ์™ธ๋ถ€ ํด๋ž˜์Šค

	class Inner {
		// ์ธ์Šคํ„ด์Šค ๋‚ด๋ถ€ ํด๋ž˜์Šค
	}

	static class StaticInner {
		// ์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค
	}

	void run() {
		class LocalInner {
		// ์ง€์—ญ ๋‚ด๋ถ€ ํด๋ž˜์Šค
		}
	}
}

 

์ธ์Šคํ„ด์Šค ๋‚ด๋ถ€ ํด๋ž˜์Šค

์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์„ ์–ธ ์œ„์น˜ ์„ ์–ธํ•˜๋ฉฐ, ๋ฉค๋ฒ„ ๋‚ด๋ถ€ ํด๋ž˜์Šค ์ด๋‹ค. ์™ธ๋ถ€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ ์™ธ๋ถ€ ์ „์—ญ๋ณ€์ˆ˜์— ์ ‘๊ทผโญ•

๋‹จ, ์ธ์Šคํ„ด์Šค ๋‚ด๋ถ€ ํด๋ž˜์Šค๋Š” ๋ฐ˜๋“œ์‹œ ์™ธ๋ถ€ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ ์ดํ›„์— ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋”ฐ๋ผ์„œ ์ •์  ๋ณ€์ˆ˜์™€ ์ •์  ๋ฉ”์„œ๋“œ๋Š” ์ธ์Šคํ„ด์Šค ๋‚ด๋ถ€ ํด๋ž˜์Šค์— ์„ ์–ธํ•  ์ˆ˜ ์—†๋‹ค!!

class Outer {//์™ธ๋ถ€ ํด๋ž˜์Šค
    private int num=1; //์™ธ๋ถ€ ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜
    private static int sNum=2; //์™ธ๋ถ€ ํด๋ž˜์Šค ์ •์  ๋ณ€์ˆ˜
    private InClass inClass;

    public Outer(){
        inClass=new InClass();
    }

    class InClass{//์ธ์Šคํ„ด์Šค ๋‚ด๋ถ€ ํด๋ž˜์Šค
        int inNum=10;//๋‚ด๋ถ€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜
				//static int sInNum=5;->์„ ์–ธ ๋ถˆ๊ฐ€
        void test(){
            //์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ ์ •์  ๋ณ€์ˆ˜์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
            System.out.println("Outer num= "+num);
            System.out.println("Outer sNum= "+sNum);
        }
    }

    public void testClass(){
        inClass.test();
    }

}

public class InnerClassExample{

    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.testClass();//๋‚ด๋ถ€ ํด๋ž˜์Šค ๊ธฐ๋Šฅ ํ˜ธ์ถœ
    }
}

 

์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค

์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค๋Š” ์™ธ๋ถ€ ํด๋ž˜์Šค์™€์˜ ์กด์žฌ์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ํ˜ธ์ถœํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์„ ์–ธ ์œ„์น˜์— ์„ ์–ธํ•œ๋‹ค. (๋ฉค๋ฒ„ ๋‚ด๋ถ€ ํด๋ž˜์Šค) ์™ธ๋ถ€ ์ „์—ญ ๋ณ€์ˆ˜์—๋งŒ ์ ‘๊ทผ โญ•.(์™ธ๋ถ€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์—๋Š” ์ ‘๊ทผ โŒ)

class Outer {//์™ธ๋ถ€ ํด๋ž˜์Šค
    private int num=1; //์™ธ๋ถ€ ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜
    private static intsNum=2; //์™ธ๋ถ€ ํด๋ž˜์Šค ์ •์  ๋ณ€์ˆ˜

    void getPrint(){
        System.out.println("์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ");
    }

    static void getPrintStatic(){
        System.out.println("์Šคํƒœํ‹ฑ ๋ฉ”์„œ๋“œ");
    }

    static class StaticInClass{//์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค
        int inNum=10;//์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜
        static int sInNum=5;//์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค์˜ ์ •์  ๋ณ€์ˆ˜
        void test(){
//            System.out.println("Outer num= "+num);-> non-static์ด์—ฌ์„œ ์•ˆ๋จ.
            System.out.println("Outer sNum= "+sNum);//์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ์ •์  ๋ณ€์ˆ˜์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
            System.out.println("In Num= "+inNum);
            System.out.println("In sInNum= "+sInNum);
//            getPrint(); -> non-static์ด์—ฌ์„œ ์•ˆ๋จ
getPrintStatic();
        }
    }

}

public class InnerClassExample{
    public static void main(String[] args) {
        Outer.StaticInClass s = new Outer.StaticInClass();
        s.test();
    }
}

 

์ง€์—ญ ๋‚ด๋ถ€ ํด๋ž˜์Šค

ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„๊ฐ€ ์•„๋‹Œ ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์ •์˜๋˜๋Š” ํด๋ž˜์Šค๋กœ, ์ง€์—ญ๋ณ€์ˆ˜์™€ ์œ ์‚ฌํ•˜๊ฒŒ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. 

class Outer {//์™ธ๋ถ€ ํด๋ž˜์Šค
    private int num1=1; //์™ธ๋ถ€ ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜
    private static intsNum=2; //์™ธ๋ถ€ ํด๋ž˜์Šค ์ •์  ๋ณ€์ˆ˜

    void test(){
        int num2=5;
        class LocalInClass{//์ง€์—ญ ๋‚ด๋ถ€ ํด๋ž˜์Šค
            private int inNum=1;
//            private static int sInNum=2; -> ์„ ์–ธ ๋ถˆ๊ฐ€
            void getPrint(){
                System.out.println("inNum= "+inNum);
                System.out.println("outNum= "+num1);
                System.out.println("methodNum= "+num2);
                System.out.println("outSnum= "+sNum);
            }

        }
        LocalInClass localInClass=new LocalInClass();
        localInClass.getPrint();
    }


}

public class InnerClassExample{
    public static void main(String[] args) {
        Outer outer=new Outer();
        outer.test();
    }
}

 

์ฐธ๊ณ  ์ž๋ฃŒ)

์ƒ์„ฑ์ž(Constructor)

https://jenkov.com/tutorials/java/constructors.html

https://www.programiz.com/java-programming/constructors

 

Java Constructors (With Examples)

Java Constructors In this tutorial, we will learn about Java constructors, their types, and how to use them with the help of examples. What is a Constructor? A constructor in Java is similar to a method that is invoked when an object of the class is create

www.programiz.com

 

 

๋‚ด๋ถ€ ํด๋ž˜์Šค(Nested Class)

https://www.javatpoint.com/java-inner-class

 

Java Inner Class - javatpoint

Java inner class or nested class with member inner class, anonymous inner class, local inner class and java static nested class.

www.javatpoint.com

https://nothing-is-on-my-way.tistory.com/30

 

๐Ÿงก์ค‘์ฒฉ ํด๋ž˜์Šค,์ธํ„ฐํŽ˜์ด์Šค & ์ต๋ช… ๊ฐ์ฒด

1๏ธโƒฃ์ค‘์ฒฉ ํด๋ž˜์Šค๋ž€? ์ค‘์ฒฉ ํด๋ž˜์Šค๋ž€ ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์„ ์–ธ๋œ ํด๋ž˜์Šค๋ฅผ ๋งํ•œ๋‹ค. ๋ณดํ†ต, ํด๋ž˜์Šค๊ฐ€ ์—ฌ๋Ÿฌ ํด๋ž˜์Šค์™€ ๊ด€๊ณ„๋ฅผ ๋งบ์„ ๊ฒฝ์šฐ์—๋Š” ๋…๋ฆฝ์ ์œผ๋กœ ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์œผ๋‚˜, ํŠน์ • ํด๋ž˜์Šค์™€

nothing-is-on-my-way.tistory.com

์ฝ”๋“œ

https://github.com/jeein2222/codeStates_practice