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 λ©€λ²λ ν΄λμ€λΉ νλλ§ μμ±λκ³ , κ°μ²΄λ€μ μν΄ κ³΅μ λλ€.
- non-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 λ©μλλ μ€μ§ static λ©€λ²λ§ μ κ·Ό κ°λ₯
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