PL(Programming Language)/Java

[Java] μžλ°”μ˜ μ ‘κ·Ό μ§€μ •μž (private, protected, public, default), static 멀버, final 클래슀

탱저 2021. 11. 4. 13:49

μ ‘κ·Ό μ§€μ •μž

  • private, protected, public, λ””ν΄νŠΈ(μ ‘κ·Ό μ§€μ •μž μƒλž΅)
  • λͺ©μ 
    • ν΄λž˜μŠ€λ‚˜ 일뢀 멀버λ₯Ό κ³΅κ°œν•˜μ—¬ λ‹€λ₯Έ ν΄λž˜μŠ€μ—μ„œ μ ‘κ·Όν•˜λ„λ‘ ν—ˆμš©
    • 객체 μ§€ν–₯ μ–Έμ–΄μ˜ μΊ‘μŠν™” 정책은 멀버λ₯Ό λ³΄ν˜Έν•˜λŠ” 것

클래슀 μ ‘κ·Ό μ§€μ •

  • λ‹€λ₯Έ ν΄λž˜μŠ€μ—μ„œ μ‚¬μš©ν•˜λ„λ‘ ν—ˆμš©ν•  μ§€ μ§€μ •
  • public 클래슀 -> λ‹€λ₯Έ λͺ¨λ“  ν΄λž˜μŠ€μ—κ²Œ μ ‘κ·Ό ν—ˆμš©
  • λ””ν΄νŠΈ 클래슀 -> μ ‘κ·Ό μ§€μ •μž μƒλž΅, package-private라고도 함, 같은 νŒ¨ν‚€μ§€μ˜ ν΄λž˜μŠ€μ—λ§Œ μ ‘κ·Ό ν—ˆμš© 
    νŒ¨ν‚€μ§€ Pμ—μ„œ Q λ‚΄μ˜ λ””ν΄νŠΈ 클래슀 C μ ‘κ·Ό λΆˆκ°€λŠ₯

맴버 μ ‘κ·Ό μ§€μ •

  • public
    • νŒ¨ν‚€μ§€μ— 관계 없이 λͺ¨λ“  ν΄λž˜μŠ€μ—μ„Έ μ ‘κ·Ό ν—ˆμš©
  • private
    • 동일 클래슀 λ‚΄μ—λ§Œ μ ‘κ·Ό ν—ˆμš©
    • 상속받은 μ„œλΈŒ ν΄λž˜μŠ€μ—μ„œ μ ‘κ·Ό λΆˆκ°€
  • protected
    • 같은 νŒ¨ν‚€μ§€ λ‚΄μ˜ λ‹€λ₯Έ λͺ¨λ“  ν΄λž˜μŠ€μ—μ„Έ μ ‘κ·Ό ν—ˆμš©
    • 상속 받은 μ„œλΈŒ ν΄λž˜μŠ€λŠ” λ‹€λ₯Έ νŒ¨ν‚€μ§€μ— μžˆμ–΄λ„ μ ‘κ·Ό κ°€λŠ₯
  • λ””ν΄νŠΈ(default)
    • 같은 νŒ¨ν‚€μ§€ λ‚΄ λ‹€λ₯Έ ν΄λž˜μŠ€μ—κ²Œ μ ‘κ·Ό ν—ˆμš©


μ•„λž˜ μ½”λ“œμ˜ 컴파일 였λ₯˜κ°€ λ‚˜λŠ” 이유?

class Sample {
	public int a;
    private int b;
    int c;
}

public class AccessEx{
	public static void main(String[] args) {
    	Sample aClass = new Sample();
        aClass.a = 10;
        aClass.b = 10; // 컴파일 였λ₯˜
        aClass.c = 10;
    }
}

-> Sample 클래슀의 a와 cλŠ” 각각 public, default μ ‘κ·Ό μ§€μ •μžλ‘œ 선언이 λ˜μ–΄ 같은 νŒ¨ν‚€μ§€μ— μ†ν•œ AccesEx ν΄λž˜μŠ€μ—μ„œ μ ‘κ·Ό κ°€λŠ₯

-> Sample 클래슀의 bλŠ” private으둜 선언이 λ˜μ–΄ AccessEx ν΄λž˜μŠ€μ—μ„œ μ ‘κ·Ό λΆˆκ°€λŠ₯


static 멀버

  • static 멀버 μ„ μ–Έ
class StaticSample {
	int n; 			// non-static ν•„λ“œ
    void g() {...}  // not-static λ©”μ†Œλ“œ
    
    static int m; 			// static ν•„λ“œ
    static void f() {...}	// static λ©”μ†Œλ“œ
}

 

  • 객체 생성과 non-static λ©€λ²„μ˜ 생성
    • non-static λ©€λ²„λŠ” 객체가 생성될 λ•Œ, κ°μ²΄λ§ˆλ‹€ 생긴닀.
    • static λ©€λ²„λŠ” ν΄λž˜μŠ€λ‹Ή ν•˜λ‚˜λ§Œ μƒμ„±λ˜κ³ , 객체듀에 μ˜ν•΄ κ³΅μœ λœλ‹€.
  • static 멀버와 non-static 멀버 νŠΉμ„±

      non-static 멀버 static 멀버
    μ„ μ–Έ class Sample {
        int n;
        void g() {...}
    }
    class Sample {
        static int m;
        static void g() {...}
    }
    곡간적 νŠΉμ„± λ©€λ²„λŠ” κ°μ²΄λ§ˆλ‹€ 별도 쑴재
     - μΈμŠ€ν„΄μŠ€ 멀버라고 뢀름
    λ©€λ²„λŠ” 클래슀 λ‹Ή ν•˜λ‚˜ 생성
     - λ©€λ²„λŠ” 객체 λ‚΄λΆ€ μ•„λ‹Œ λ³„λ„μ˜ 곡간에 생성
     - 클래슀 멀버라고 뢀름
    μ‹œκ°„μ  νŠΉμ„± 객체 생성 μ‹œμ— 멀버 생성
     - 객체가 생길 λ•Œ 멀버도 생성
     - 객체 생성 ν›„ 멀버 μ‚¬μš© κ°€λŠ₯
     - 객체 사라지면 멀버도 사라짐
    클래슀 λ‘œλ”© μ‹œμ— 멀버 생성
     - 객체 생기기 전에 이미 생성
     - 객체 생기기 전에도 μ‚¬μš© κ°€λŠ₯
     - 객체가 사라져도 λ©€λ²„λŠ” 사라지지 μ•ŠμŒ
     - λ©€λ²„λŠ” ν”„λ‘œκ·Έλž¨μ΄ μ’…λ£Œλ  λ•Œ 사라짐
    곡유의 νŠΉμ„± κ³΅μœ λ˜μ§€ μ•ŠμŒ
     - λ©€λ²„λŠ” 객체 내에 각각 곡간 μœ μ§€
    λ™μΌν•œ 클래슀의 λͺ¨λ“  객체듀에 μ˜ν•΄ 곡유
  • static 멀버 μ‚¬μš©
    • 클래슀 μ΄λ¦„μœΌλ‘œ μ ‘κ·Ό κ°€λŠ₯ 
    • StaticSample.m = 3; // 클래슀 μ΄λ¦„μœΌλ‘œ static ν•„λ“œ μ ‘κ·Ό StaticSample.f(); // 클래슀 μ΄λ¦„μœΌλ‘œ static λ©”μ†Œλ“œ ν˜ΈμΆœβ€‹
    • 객체의 λ©€λ²„λ‘œ μ ‘κ·Ό κ°€λŠ₯
      StaticSample b1 = new StaticSample();
      
      b1.m = 3; // 객체 μ΄λ¦„μœΌλ‘œ static ν•„λ“œ μ ‘κ·Ό
      b1.f();   // 객체 μ΄λ¦„μœΌλ‘œ static λ©”μ†Œλ“œ ν˜ΈμΆœβ€‹
    • non-static λ©€λ²„λŠ” 클래슀 μ΄λ¦„μœΌλ‘œ μ ‘κ·Ό λΆˆκ°€λŠ₯

  • static의 ν™œμš©
    • μ „μ—­ λ³€μˆ˜μ™€ μ „μ—­ ν•¨μˆ˜λ₯Ό λ§Œλ“€ λ•Œ
    • 곡유 멀버λ₯Ό λ§Œλ“€κ³ μž ν•  λ•Œ
  • static λ©”μ†Œλ“œμ˜ μ œμ•½ 쑰건
    • static λ©”μ†Œλ“œλŠ” 였직 static λ©€λ²„λ§Œ μ ‘κ·Ό κ°€λŠ₯
      • 객체가 μƒμ„±λ˜μ§€ μ•Šμ€ μƒν™©μ—μ„œλ„ static λ©”μ†Œλ“œλŠ” 싀행될 수 있기 λ•Œλ¬Έμ—, non-static 멀버 ν™œμš© λΆˆκ°€
      • non-static λ©”μ†Œλ“œλŠ” static 멀버 μ‚¬μš© κ°€λŠ₯
        class StaticMethod {
        	int n;
            void f1(int x) {n = x;} // 정상
            void f2(int x) {m = x;} // 정상
            static int m;
            static void s1(int x) {n = x;} // 컴파일 였λ₯˜, static λ©”μ†Œλ“œλŠ” non-static ν•„λ“œ n μ‚¬μš© λΆˆκ°€
            static void s2(int x) {f1(3);} // 컴파일 였λ₯˜, static λ©”μ†Œλ“œλŠ” non-static λ©”μ†Œλ“œ f1() μ‚¬μš© λΆˆκ°€
            static void s3(int x) {m = x;} // 정상, static λ©”μ†Œλ“œλŠ” static ν•„λ“œ m μ‚¬μš© κ°€λŠ₯
            static void s4(int x) {s3(3);} // 정상, static λ©”μ†Œλ“œλŠ” static λ©”μ†Œλ“œ s3() 호좜 κ°€λŠ₯
        }​
      • static λ©”μ†Œλ“œλŠ” this μ‚¬μš© λΆˆκ°€ -> static λ©”μ†Œλ“œλŠ” 객체 없이도 μ‚¬μš© κ°€λŠ₯ν•΄ this 레퍼런슀 μ‚¬μš©ν•  수 μ—†λ‹€!
        static void f() {this.n = x;} // 였λ₯˜. static λ©”μ†Œλ“œμ—μ„œ this μ‚¬μš© λΆˆκ°€
        static void g() {this.m = x;} // 였λ₯˜. static λ©”μ†Œλ“œμ—μ„œ this μ‚¬μš© λΆˆκ°€β€‹

static 멀버λ₯Ό κ°€μ§„ Calc 클래슀 μž‘μ„± - μ „μ—­ ν•¨μˆ˜λ‘œ μž‘μ„±ν•˜κ³ μž ν•˜λŠ” abs, max, min 3개 ν•¨μˆ˜λ₯Ό static λ©”μ†Œλ“œλ₯Ό μž‘μ„±ν•˜κ³  ν˜ΈμΆœν•˜λŠ” μ‚¬λ‘€λ‘œ 보여라.

class Calc{
	public static int abs(int a) {
		return a > 0? a : -a; // a>0이면 a, μ•„λ‹ˆλ©΄ -a 쑰건문
    }
	public static int max(int a , int b) {
		return (a > b)? a : b;
	}
	public static int min(int a , int b) {
		return (a > b)? b: a;
	}
}

public class CalcEx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(Calc.abs(-5));
		System.out.println(Calc.max(10, 8));
		System.out.println(Calc.min(-3, -8));
	}

}

Calc ν΄λž˜μŠ€μ—μ„œ static을 μ§€μš°λ©΄ μ „μ—­ ν•¨μˆ˜μ˜ μ—­ν•  λͺ»ν•¨ -> μ—λŸ¬ λ°œμƒ


final ν΄λž˜μŠ€μ™€ λ©”μ†Œλ“œ

  • final 클래슀 - 더 이상 클래슀 상속 λΆˆκ°€λŠ₯
    final class FinalClass {
    	....
    }
    
    class DerivedClass extends FinalClass {
    	// 컴파일 였λ₯˜
    }​
  • final λ©”μ†Œλ“œ - 더 이상 μ˜€λ²„λΌμ΄λ”© λΆˆκ°€λŠ₯
    public class SuperClass {
    	protected final int finalMethod() {....}
    }
    
    class subClass extends SuperClass {
    	protected int finalMethod() {....} // 컴파일 였λ₯˜. μ˜€λ²„λΌμ΄λ”© λΆˆκ°€λŠ₯
    }​

 

final ν•„λ“œ

  • final ν•„λ“œ, μƒμˆ˜ μ„ μ–Έ
    // μƒμˆ˜ μ„ μ–Έ μ‹œ μ‚¬μš©
    class SharedClass {
    	public static final double PI = 3.14;
    }​
  • μƒμˆ˜ ν•„λ“œλŠ” μ„ μ–Έ μ‹œμ— 초기 κ°’ 지정해야함
  • μƒμˆ˜ ν•„λ“œλŠ” μ‹€ν–‰ 쀑 κ°’ λ³€κ²½ λΆˆκ°€λŠ₯
728x90