🐯[TIL] 250616-011

byoΒ·2025λ…„ 6μ›” 16일

πŸ’« JAVA

βœ”οΈ static

βœ… μ •μ˜

  • static은 ν΄λž˜μŠ€μ— μ†ν•˜λŠ” 정적 멀버
  • μΈμŠ€ν„΄μŠ€ 없이 클래슀λͺ….λ©”μ†Œλ“œλ‘œ 호좜 κ°€λŠ₯
  • JVM이 클래슀 νŒŒμΌμ„ λ©”λͺ¨λ¦¬μ— 올릴 λ•Œ κ³ μ •λœ μœ„μΉ˜μ— λ”± ν•œ 번 μƒμ„±λ˜μ–΄ λͺ¨λ“  μΈμŠ€ν„΄μŠ€κ°€ κ³΅μœ ν•¨.

↔️ μΈμŠ€ν„΄μŠ€ vs static

κ΅¬λΆ„μΈμŠ€ν„΄μŠ€ 멀버static 멀버
μ†Œμ†κ°μ²΄(μΈμŠ€ν„΄μŠ€)에 μ†ν•¨ν΄λž˜μŠ€ μžμ²΄μ— 속함
μ ‘κ·Ό 방법객체참쑰.ν•„λ“œ, λ©”μ†Œλ“œ()클래슀λͺ….ν•„λ“œ, λ©”μ†Œλ“œ()
λ©”λͺ¨λ¦¬ μœ„μΉ˜Heap μ˜μ—­Method Area μ˜μ—­
생성 μ‹œμ κ°μ²΄ 생성 μ‹œν΄λž˜μŠ€ λ‘œλ”© μ‹œ
μ‚¬μš© λͺ©μ κ° κ°μ²΄λ§ˆλ‹€ κ³ μœ ν•œ 데이터곡톡 데이터/κΈ°λŠ₯ 곡유 λͺ©μ 

-memory-

Class Loader

JVM Memory

Method Area Heap JVM Language Stacks PC Registers Native Method Stacks

Execution Engine <-> Native Method <-> Native Method Libraries



class Person {
    public static void greet() {
        System.out.println("Hello from static method");
    }
    public void sayHello() {
        System.out.println("Hello from instance method");
    }
}
public class Main {
    public static void main(String[] args) {
        Person.greet();
        Person p = new Person();
        p.sayHello();
    }
}

β—μ‚¬μš© μ‹œ μ£Όμ˜μ‚¬ν•­

  • κ³Όλ„ν•œ static은 λ©”λͺ¨λ¦¬ λ‚­λΉ„ 유발
  • static λ©”μ†Œλ“œμ—μ„œλŠ” μΈμŠ€ν„΄μŠ€ λ³€μˆ˜μ— μ ‘κ·Ό λΆˆκ°€
  • 객체 μƒνƒœμ™€ λ¬΄κ΄€ν•œ κΈ°λŠ₯μ—λ§Œ μ‚¬μš©ν•΄μ•Ό 함

πŸ” 핡심 μš”μ•½ 정리

ν‚€μ›Œλ“œμ„€λͺ… μš”μ•½
staticν΄λž˜μŠ€μ— μ†Œμ†λ˜λŠ” κ³ μ • 멀버 (μΈμŠ€ν„΄μŠ€ 없이 μ ‘κ·Ό κ°€λŠ₯)
μ ‘κ·Ό λ°©μ‹ν΄λž˜μŠ€λͺ….λ©”μ†Œλ“œ() λ˜λŠ” 클래슀λͺ….ν•„λ“œ
μž₯점메λͺ¨λ¦¬ 곡유, μ ‘κ·Ό 속도 빠름, 곡톡 κΈ°λŠ₯ μ²˜λ¦¬μ— 적합
μ£Όμ˜μ‚¬ν•­μΈμŠ€ν„΄μŠ€ λ³€μˆ˜μ— μ ‘κ·Όν•  수 μ—†μœΌλ©°, λ‚¨μš© μ‹œ λ©”λͺ¨λ¦¬ λΆ€λ‹΄

βœ”οΈ inner class

βœ… μ •μ˜

  • 클래슀 μ•ˆμ— μ„ μ–Έλœ 또 λ‹€λ₯Έ 클래슀
  • 논리적 κ·Έλ£Ήν™”, μΊ‘μŠν™” κ°•ν™” λͺ©μ 
  • Outer Class의 멀버에 자유둭게 μ ‘κ·Ό κ°€λŠ₯

ℹ️ outer class μ™€μ˜ 관계

  • Inner ClassλŠ” Outer Class의 private 멀버에도 μ ‘κ·Ό κ°€λŠ₯
  • 마치 같은 클래슀 λ‚΄λΆ€μ²˜λŸΌ μž‘λ™. 논리적 λ‹¨μœ„. 동일 클래슀 파일둜 μΈμ‹ν•œλ‹€.
  • inner ClassλŠ” 1개의 Outer Class instance에 속할 수 μžˆλ‹€.

πŸ€” Why Inner Class?

  • κ΄€λ ¨ μžˆλŠ” 클래슀λ₯Ό λ…Όλ¦¬μ μœΌλ‘œ λ¬Άμ–΄μ€Œ
  • μ™ΈλΆ€ 클래슀 멀버λ₯Ό 자유둭게 μ‚¬μš©ν•  수 μžˆμ–΄ 편리
  • μΊ‘μŠν™”μ™€ 가독성 ν–₯상

class Car {
    String brand;
    public Car(String brand) {
        this.brand = brand;
    }
    class Engine {
        public void start() {
            System.out.println("The engine of " + brand + " is starting...");
        }
    }
    public void startEngine() {
        Engine engine = new Engine();
        engine.start();
    }
}
public class Main {
    public static void main(String[] args) {
        Car car = new Car("BMW");
        car.startEngine();
    }
}

βœ… engine μž¬ν™œμš© κ°€λŠ₯ 버전

class Car {
    String brand;
    Engine engine;
    public Car(String brand) {
        this.brand = brand;
        this.engine = new Engine();
    }
    class Engine {
        public void start() {
            System.out.println("The engine of " + brand + " is starting...");
        }
    }
    public void startEngine() {
        engine.start();
    }
}
public class Main {
    public static void main(String[] args) {
        Car car = new Car("BMW");
        car.startEngine();
    }
}

βœ… inner class new ν‚€μ›Œλ“œλ‘œ 생성

// 1. Outer instance 생성
        Main main = new Main();
        // 2. Inner instance 생성 (outer.new μ‚¬μš©)
        Main.Inner inner = main.new Inner();
        // 3. Inner 클래슀 λ©”μ„œλ“œ 호좜
        inner.showMessage();

βœ… κ°•ν•œ κ²°ν•© 없이 클래슀 κ°„ ν˜‘λ ₯


class Button {
    interface OnClickListener {
        void onClick();
    }
    private OnClickListener listener;

    public void setOnclickListener(OnClickListener listener) {
        this.listener = listener;
    }
    public void click() {
        if (listener != null) {
            listener.onClick();
        }
    }
    // OnclickListenerλ₯Ό κ΅¬ν˜„ν•œ ClickHandler Innerclass
    private class ClickHandler implements OnClickListener {
        public void onClick() {
            System.out.println("Button is Clicked");
        }
    }
    public void simulateClick() {
        setOnclickListener(new ClickHandler());
        click();
    }
}
public class Main {
    public static void main(String[] args) {
        Button button = new Button();
        button.simulateClick();
    }
}

βœ… μ΄λ„ˆ ν΄λž˜μŠ€μ™€ λ©”λͺ¨λ¦¬

  • Inner ClassλŠ” Outer μΈμŠ€ν„΄μŠ€μ™€ ν•¨κ»˜ λ©”λͺ¨λ¦¬μ— 쑴재
  • Outer 객체가 GC될 λ•Œ Inner도 ν•¨κ»˜ 정리됨
public class Outer {
    private String data = "Outer data";
    class Inner {
        void printData() {
            System.out.println(data);
        }
    }
    public Inner createInner() {
        return new Inner();
    }
    public static void main(String[] args) {
        Outer outer = new Outer();
        Outer.Inner inner = outer.createInner();
        inner.printData();
        outer = null; // outer μΈμŠ€ν„΄μŠ€κ°€ null 이 λ˜μ–΄λ„ Inner μΈμŠ€ν„΄μŠ€λ₯Ό μ°Έμ‘°ν•˜κ³  μžˆλŠ” inner λŠ” 사라지지 μ•ŠλŠ”λ‹€.
        inner.printData();
        inner = null;
        System.gc();
    }
}


class Outer {
    static class StaticInner {
        void hello() {
            System.out.println("Hi!");
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Outer.StaticInner inner = new Outer.StaticInner();
        inner.hello();
    }
}

Inner vs Static Nested Class

ꡬ뢄Inner ClassStatic Nested Class
Outer μΈμŠ€ν„΄μŠ€ ν•„μš”OX
μ ‘κ·Ό κ°€λŠ₯ 멀버Outer의 λͺ¨λ“  멀버static λ©€λ²„λ§Œ
μ‚¬μš© μ˜ˆμ‹œλ©€λ²„ λ„μš°λ―Έ ν΄λž˜μŠ€μœ ν‹Έλ¦¬ν‹°, ꡬ쑰 뢄리
λ©”λͺ¨λ¦¬ 관리Outer 객체와 ν•¨κ»˜λ…λ¦½ 객체둜 관리

βœ”οΈ Anonymous Class

βœ… μ •μ˜

  • 이름이 μ—†λŠ” μΌνšŒμ„± 클래슀
  • 주둜 μΈν„°νŽ˜μ΄μŠ€λ‚˜ 좔상 클래슀λ₯Ό μ¦‰μ‹œ κ΅¬ν˜„ν•  λ•Œ μ‚¬μš©
  • μž¬μ‚¬μš©μ€ μ–΄λ ΅μ§€λ§Œ κ°„λ‹¨ν•œ κ΅¬ν˜„μ— 유용

βœ… interface Greeting μž‘μ„±

  • 읡λͺ… ν΄λž˜μŠ€λŠ” 기반이 λ˜λŠ” μΈν„°νŽ˜μ΄μŠ€κ°€ ν•„μš”
  • void greet() λ©”μ†Œλ“œλ§Œ μ„ μ–Έ

βœ… 읡λͺ… 클래슀 생성 방법

  • new μΈν„°νŽ˜μ΄μŠ€λͺ…() { κ΅¬ν˜„ } ν˜•νƒœλ‘œ μ •μ˜
  • μ€‘κ΄„ν˜Έ λ‚΄μ—μ„œ λ©”μ†Œλ“œλ₯Ό μ¦‰μ‹œ μ˜€λ²„λΌμ΄λ”©

interface Greeting {
    void greet();
}
class GClass implements Greeting {
    public void greet() {}
}
public class Main {
    public static void main(String[] args) {
    	// GClass λ₯Ό κ±°μΉ˜μ§€ μ•Šκ³  λ°”λ‘œ μ„ μ–Έκ³Ό κ΅¬ν˜„μ„ λ™μ‹œμ— ν•˜λŠ” anonymoust class Greeting
        Greeting greeting = new Greeting() {
            public void greet() {
                System.out.println("Hello World");
            }
        };
        greeting.greet();
    }
}

interface Calculator {
    int compute(int a, int b);
}
class CalculatorMachine implements Calculator {
    @Override
    public int compute(int a, int b) {
        return a * b;
    }
}
public class Main {
    public static void main(String[] args) {
        // 1. κ°„λ‹¨ν•˜κ²Œ κΈ°λŠ₯을 κ΅¬ν˜„ν•΄μ„œ λ°”λ‘œ ν…ŒμŠ€νŠΈ ν•˜λ €λ©΄ anonymous classλ₯Ό μ‚¬μš©ν•˜λ©΄ λœλ‹€.
        Calculator multiply = new Calculator() {
            public int compute(int a, int b) {
                return a * b;
            }
        };
        Calculator add = new Calculator() {
            public int compute(int a, int b) {
                return a + b;
            }
        };
        // 숫자λ₯Ό 문자둜 λ°›μ•„ ν•©μΉœ λ’€ λ‹€μ‹œ 숫자둜 λ°˜ν™˜ν•˜λŠ” connectNum κΈ°λŠ₯
        Calculator connectNum = new Calculator() {
            public int compute(int a, int b) {
                return Integer.parseInt("" + a + b);
            }
        };
        System.out.println(multiply.compute(3, 4)); //12
        System.out.println(add.compute(3, 4)); //7
        System.out.println(connectNum.compute(3, 4)); //34

        // 2. λ§Œμ•½ κΈ°λŠ₯을 μ—¬λŸ¬ 번 μ‚¬μš©ν•˜κ³  μ‹ΆμœΌλ©΄ class에 implement ν•΄μ„œ κ·Έ instance λ₯Ό κ°€μ§€κ³  μ‹€ν–‰μ‹œν‚€λŠ” 것이 λ‚«λ‹€.
        CalculatorMachine machine = new CalculatorMachine();
        System.out.println(machine.compute(1, 2)); //2
        System.out.println(machine.compute(10, 20)); //200
    }
}

λžŒλ‹€μ‹κ³Ό 비ꡐ

  • μžλ°” 8λΆ€ν„°λŠ” 읡λͺ… ν΄λž˜μŠ€λ³΄λ‹€ λžŒλ‹€μ‹μ„ 더 자주 μ‚¬μš©
  • 읡λͺ… ν΄λž˜μŠ€λŠ” μ—¬μ „νžˆ λ³΅μž‘ν•œ κ΅¬ν˜„μ΄λ‚˜ μƒνƒœκ°€ μžˆλŠ” 경우 유용

읡λͺ… 클래슀 vs λžŒλ‹€μ‹

ν•­λͺ©μ΅λͺ… ν΄λž˜μŠ€λžŒλ‹€μ‹
μ΄λ¦„μ—†μŒμ—†μŒ
κ΅¬ν˜„ λŒ€μƒμΈν„°νŽ˜μ΄μŠ€/좔상 ν΄λž˜μŠ€ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€
μ½”λ“œ 길이비ꡐ적 κΈΈλ‹€κ°„κ²°ν•˜λ‹€
μƒνƒœ λ³΄μœ κ°€λŠ₯μ œν•œμ 

πŸ’« CS

βœ”οΈ CPU λ™μž‘μ›λ¦¬


class CPU {
    public int register = 0; // register μž₯치
    public int[] memory = {4, 5, 6}; // memory
    int pc = 0;
    private static final int LOAD = 1;
    private static final int SAVE = 2;
    private static final int ADD = 3;
    private static final int SUB = 4;
    private static final int HALT = 5;
    // assembly
    private int[][] program = { // { operator , memory location }
            { LOAD, 0 },         // register = memory[0]
            { ADD, 1 },          // register = register + memory[1]
            { SAVE, 2 },         // memory[2] = register
            { HALT, 0 }          // stop program execution
    };
    public void executeProgram() {
        while(true) {
            int opcode = program[pc][0];  // {LOAD, ADD, SAVE, HALT}
            int operand = program[pc][1]; // {0, 1, 2, 0}
            switch (opcode) {
                case LOAD:
                    register = memory[operand];
                    System.out.println("MOV: Loading memory[" + operand + "] (" + memory[operand] + ") to Register.");
                    System.out.println("Register : " + register);
                    break;
                case SAVE:
                    memory[operand] = register;
                    System.out.println("MOV: Saving memory[" + operand + "] (" + memory[operand] + ") to Register.");
                    System.out.println("Register : " + register);
                    break;
                case ADD:
                    register += memory[operand];
                    System.out.println("MOV: Adding memory[" + operand + "] (" + memory[operand] + ") to Register.");
                    System.out.println("Register : " + register);
                    break;
                case SUB:
                    register -= memory[operand];
                    System.out.println("MOV: Subtracting memory[" + operand + "] (" + memory[operand] + ") from Register.");
                    System.out.println("Register : " + register);
                    break;
                case HALT:
                    System.out.println("MOV: Stopping program.");
                    return;
                default:
                    System.out.println("Unknown instruction. Halting... opcode : " + opcode);
                    return;
            }
            pc++;
        }
    }
}
public class Main {
    public static void main(String[] args) {
        CPU cpu = new CPU();
        cpu.executeProgram();
        System.out.println("Final memory state: memory[" + (cpu.pc-1) + "] = " + cpu.memory[cpu.pc-1]);
    }
}
profile
πŸ—‚οΈ hamstern

0개의 λŒ“κΈ€