๋์ ์ผ๋ก ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ ์์ญ
๊ฐ์ฒด ์์ฑ : ํด๋์ค๋ช
+ ๋ณ์๋ช
= new ํด๋์ค๋ช
() ;
ScopeEx01 sc = new ScopeEx01();
public class ScopeEx02 {
int n1 = 1;
static int n2 = 2;
static void m1() {
int n3 = 10;
System.out.println("m1:" + n3);
}
void m2() {
System.out.println("m2:" + n1);
}
public static void main(String[] args) {
ScopeEx01 sc = new ScopeEx01();
System.out.println(sc.n1);
sc.m2();
}
}

์์ ์ฝ๋์์ ๋ณ์๋ ์ด๋ฐ ๊ณต๊ฐ์ ํ ๋น๋๋ค.
heap ์ ์ด์ฉํ๊ธฐ ์ํด๋ new ๋ฅผ ์ฌ์ฉํ๋ค.
ScopeEx01 sc = new ScopeEx01();
์ด๋ ๊ฒ ๋์ ํ ๋น์ด ๋๋ฉด static ์ด ์๋ ๋ชจ๋ ๋ณ์๋ฅผ ์ด์ฉํ ์ ์๋ค.

๋ฉ์ธ stack์ ํ ๋น๋ ๋ณ์ sc๋ ๊ฐ์ ๊ฐ์ง๊ณ ์์ง ์๊ณ heap ์ ์๋ ๊ฐ์ ์ฃผ์๋ฅผ ๊ฐ์ง๊ณ ์๋ค. ๊ทธ๋์ ์ฐธ์กฐ๋ณ์๋ผ๊ณ ๋ถ๋ฅธ๋ค.
Share article