getter, setter / static, instance


Getter / Setter


  • κ²Œν„°(Getter)
    • ν•„λ“œκ°’ λ°˜ν™˜
    • μ ‘κ·Ό μ œμ–΄ μˆ˜μ‹μ–΄ : public
    • κ²Œν„°λͺ… : getν•„λ“œλͺ… (ν•„λ“œλͺ…μ˜ μ²«κΈ€μžλŠ” λŒ€λ¬Έμž)
    • λ§€κ°œλ³€μˆ˜ μ—†μŒ
    • λ‚΄μš© : return ν•„λ“œ;
  • μ„Έν„°(Setter)
    • ν•„λ“œ μ΄ˆκΈ°ν™”
    • μ ‘κ·Ό μ œμ–΄ μˆ˜μ‹μ–΄ : (거의) public
    • λ°˜ν™˜μžλ£Œν˜• : void (λ°˜ν™˜κ°’ X)
    • μ„Έν„°λͺ… : setν•„λ“œλͺ… (ν•„λ“œλͺ…μ˜ μ²«κΈ€μžλŠ” λŒ€λ¬Έμž)
    • λ§€κ°œλ³€μˆ˜ : ν•„λ“œμ™€ 동일
    • λ‚΄μš© : μƒμ„±μžμ™€ 동일 (this.ν•„λ“œλͺ… = λ§€κ°œλ³€μˆ˜λͺ…;)



static / instance


  • static λ³€μˆ˜
    • = ν΄λž˜μŠ€λ³€μˆ˜ = μ •μ λ³€μˆ˜
    • 처음 ν”„λ‘œκ·Έλž¨μ΄ λ©”λͺ¨λ¦¬μ— λ‘œλ”©λ  λ•Œ λ©”λͺ¨λ¦¬ ν• λ‹Ή
    • μΈμŠ€ν„΄μŠ€ 생성과 상관 없이 μ‚¬μš© κ°€λŠ₯ > 클래슀 μ΄λ¦„μœΌλ‘œ 직접 μ°Έμ‘°
    • 곡톡속성
      • μΈμŠ€ν„΄μŠ€ λ³€μˆ˜ : κ°œλ³„ 속성, 객체 생성 ν›„ μ‚¬μš© κ°€λŠ₯
  • Static Method
    • = 클래슀 λ©”μ„œλ“œ
    • 객체 생성 X, β€œν΄λž˜μŠ€μ΄λ¦„.λ©”μ„œλ“œλͺ…()β€μœΌλ‘œ 호좜
    • μΈμŠ€ν„΄μŠ€λ©€λ²„μ™€ κ΄€λ ¨ μ—†λŠ” μž‘μ—… > λ©”μ„œλ“œ 내에 μΈμŠ€ν„΄μŠ€ λ³€μˆ˜ μ‚¬μš© X, μΈμŠ€ν„΄μŠ€ λ©”μ„œλ“œ 호좜 X
  • Instance Method
    • μΈμŠ€ν„΄μŠ€(객체) 생성 ν›„ β€œμ°Έμ‘°λ³€μˆ˜.λ©”μ„œλ“œλͺ…()β€μœΌλ‘œ 호좜
    • μΈμŠ€ν„΄μŠ€ 멀버와 κ΄€λ ¨λœ μž‘μ—… > λ©”μ„œλ“œ 내에 μΈμŠ€ν„΄μŠ€ λ³€μˆ˜ μ‚¬μš© κ°€λŠ₯


public class Test {

	int a, b;   //μΈμŠ€ν„΄μŠ€λ³€μˆ˜

	int add(){  //μΈμŠ€ν„΄μŠ€λ©”μ„œλ“œ
	    return a + b;   //μΈμŠ€ν„΄μŠ€λ³€μˆ˜
	}

	static int add(int a, int b){   //ν΄λž˜μŠ€λ©”μ„œλ“œ, μ§€μ—­λ³€μˆ˜
	    return a + b;   //μ§€μ—­λ³€μˆ˜
	}
}


public class Main {

	public static void main(String[] args) {

		//ν΄λž˜μŠ€λ©”μ„œλ“œ 호좜
		System.out.println(Test.add(10, 20));

		Test test = new Test(); //μΈμŠ€ν„΄μŠ€μƒμ„±
		test.a = 30;
		test.b = 50;
		System.out.println(test.add());
	
	}

}

Categories:

Java