โ
Block
1
2
3
4
5
| // ๐ก { } ๋ก ๋ธ๋ก ์์ฑ
{
int x = 1; //ํ ์ค ํ ์ค์ด statement
System.out.println(x);
}
|
1
2
3
4
5
6
7
8
9
| int z =1;
//block
for (int i = 0; i < 5; i++) {
System.out.println(z + i); //statement
// โ ๏ธ ๋ธ๋ก ๋ฐ์์ ์ ์ธ๋ ๊ฒ์ ์์์ ์ฌ์ ์ธ ๋ถ๊ฐ
int z = 2; //๋ถ๊ฐ
}
// โ ๏ธ ๋ธ๋ก ์์์ ์ ์ธ๋ ๊ฒ์ ๋ฐ์์ ์ฌ์ฉ ๋ถ๊ฐ
System.out.println(i); //๋ถ๊ฐ
|
โ
Scope
ํด๋น ๋ณ์๊ฐ ์ ํจํ ๋ฒ์
- ํด๋์ค ๋ฉ์๋์์๋ ์ธ์คํด์ค ๋ณ์ ์ฌ์ฉ ๋ถ๊ฐ
- ํด๋์ค ๋ด ํ๋์ ์ค์ฝํ : ํด๋น ํด๋์ค ์
- ๋ฉ์๋ ๋ด ๋ณ์์ ์ค์ฝํ : ํด๋น ๋ฉ์๋ ์
๋ณ์๊ฐ ์ฌ์ฉ๋๋ ๋ฒ์
์ผ๋ฐ์ ์ผ๋กํ๋ ํ block ์ด ์๋ก์ด scope
block๋ด์์ ์ ์ธ๋ ๋ณ์๋ block์ ๋ฒ์ด๋๋ฉด ์ฌ์ฉ ๋ถ๊ฐ
๋ฐ๊นฅ scope์์ ๋ง๋ค์ด์ง ๋ณ์๋ ๋ ๋ค๋ฅธ scope์์์ ์ฌ์ฉ ๊ฐ๋ฅํ์ง๋ง,
ํ block๋ด์์ ๋ง๋ค์ด์ง ๋ณ์๋ ๊ทธ block์ ๋ฒ์ด๋๋ฉด ์ฌ์ฉ ๋ถ๊ฐ
scope ์ธ๋ถ์์ ์ ์ธ๋ ๋ณ์๋ฅผ ๋ด๋ถ์ ๋ ์ ์ธ ๋ถ๊ฐ๋ฅ
๊ทธ๋ฌ๋ ์ธ์คํด์ค์ ํ๋๋ scope๋ด์์ ๋ณ์๋ก ์ ์ธ ๊ฐ๋ฅ
Static ๐ Instance ๐ Local
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| public class Variable {
//static ํด๋์ค ๋ณ์
// ํ๋ก๊ทธ๋จ์ด ์ฒ์ ์์๋ ๋ ์์ฑ, ํ๋ก๊ทธ๋จ ๋๋๊ณ ๋ฉ๋ชจ๋ฆฌ ํด์ ํ ๋ ์๋ฉธ
private static int classVariable;
//์ธ์คํด์ค ๋ณ์
// ๊ฐ์ฒด์ ์ํ ๋ณ์
// ํ์ด ์์ฑ๋๊ณ JVM์์ ์ฌ๋ผ๊ฐ
private int instanceVariable1;
//๋ก์ปฌ ๋ณ์(์ง์ญ๋ณ์)
// ๋ฉ์๋์์ ์ฐ์ด๊ณ ์ฌ๋ผ์ง๋ ๋ณ์
// ๋ฉ์๋ ์์์๋ง ์๋ฏธ๊ฐ ์์ง, ๋ฐ์์๋ ์๋ฏธ๊ฐ ์์
public void saySomething(int lcoalVarable){
int localV= 3;
}
}
|
โ
Program Arguments(Command Line Arguments)
1
2
| public static void main(String[] args) {
}
|
String[] args
๐ฐ JAVA Program Arguments
๐ฐ Command Line Arguments
๐ฐ ์๋ฐ ๋ช
๋ น ๋งค๊ฐ๋ณ์
์๋ฐ ์คํ ์, ์ธ๋ถ์์ ์ ๋ฌ๋ฐ๋ ๋ณ์
Command Line(terminal)์์ ์ฃผ๋ ๋ช
๋ น์ด args์ ์ ์ฅ๋์ด์ ์ด๋ฆ์ด ๋ช
๋ น ๋งค๊ฐ๋ณ์์.
args๋ผ๋ ๋ฐฐ์ด์ string๋ฐฐ์ด์ด๋ค.
์ธํ
๋ฆฌ์ ์ด โถ๏ธ ๋๋ฅด๊ณ edit configuration
ํ๊ณ ์ธ๋ถ์์ args ๋ฃ์ ์ ์๋ค.
โ
Package
ํด๋์ค ์์์ ํจํค์ง ์ ๋ณด ๊ฐ์ ธ์ ๋ช
์
1
| package sec06.chap02.pkg4;
|
ํจํค์ง ๊ตฌ์กฐ๋ฅผ ์ค๋ช
ํจ.
ํจํค์ง๋ฅผ importํด ์ฃผ์ด์ผ ํ๋ค.
ํด๋์ค ์ด๋ฆ์ด ๊ฐ๋๋ผ๋ ๋ค๋ฅธ ํจํค์ง ์์ ๋ค์์ผ๋ฉด ์ฌ์ฉ ๊ฐ๋ฅ
ํด๋์ค ์ด๋ฆ์ด ๋๊ฐ์๋ ์ด๋ ํจํค์ง์ ๋ค์ด์๋์ง ์๊ธฐ ์ํด ์ฌ์ฉ
ํจํค์ง์ ๋ฐ๋ผ access modifier๋ ๋ฌ๋ผ์ง๋ค.
ํจํค์ง์ ๋ฐ๋ผ์ access modifier ๋ฌ๋ผ์ง๋ค.
์๋ฅผ ๋ค์ด protected์ด๋ฉด ๊ฐ์ ํจํค์ง ๋๋ ์์ ๊ด๊ณ ์์์๋ ์ฌ์ฉ ๊ฐ๋ฅ.
- private: ์ค๋ก์ง ๊ฐ์ class ๋ด์์
- default: ๊ฐ์ ํจํค์ง ์์์
- protected: ์์ ๊ด๊ณ๋ฉด ๊ฐ๋ฅ(๋ค๋ฅธ ํจํค์ง์ผ ๋๋)
โญ๏ธ ๋ค๋ฅธ ํจํค์ง์ ์๋ ํด๋์ค๋ ์์ํ ์ ์๋ค.
๋ค๋ฅธ ํจํค์ง์์ ๊ฐ์ ธ์จ ํด๋์ค๋ก๋ถํฐ๋ ์์๋ฐ์ ์ ์๋ค.
์๋จ์ importํ๋ฉด ๊ฐ๋ฅ
1
2
| import sec06.chap02.pkg1.Parent; // pkg1์์ ์๋ Parent ํด๋์ค ๊ฐ์ ธ์ค๊ธฐ
import sec06.chap02.pkg2.* //โญ๏ธ ์์ผ๋์นด๋ *: pkg2์์ ์๋ ๋ชจ๋ ํด๋์ค ๊ฐ์ ธ์ค๊ธฐ
|
protected access modifier๋ง ์ฌ์ฉํ ์ ์์ ๊ฒ์ด๋ค.
ํจํค์ง๋ ๋ค๋ฅด์ง๋ง, ์์ ๊ด๊ณ๋ ๋ง์ผ๋๊น
ํจํค์ง ์์ ์๋ ๋ชจ๋ ํด๋์ค ๊ฐ์ ธ์ *
1
| import sec06.chap02.pkg3.* //pkg3์์ ์๋ ๋ชจ๋ ํด๋์ค ๊ฐ์ ธ์ค๊ธฐ
|
์๋ก ๋ค๋ฅธ ํจํค์ง ์์ ์๋ ๋๋ช
ํด๋์ค ๋ถ๋ฌ์ฌ ๊ฒฝ์ฐ
์ธ์คํด์ค ์ ์ธํ ๋ ์ฃผ์ํด์ผ ํจ! ์ด๋ ํจํค์ง์ ํด๋์ค์ธ์ง ๋ช
์ํ๊ธฐ
1
2
3
4
5
6
7
| //ํ์ฌ pkg1์์๋ Childํด๋์ค ์๊ณ , pkg2์์๋ Childํด๋์ค ์๋ ์ํฉ
//โ ์ด๋ ๊ฒ ํ๋ฉด ์ด๋ค ํจํค์ง์ Child์ธ์ง ์ ์๊ฐ ์์
Child child1 = new Child();
// โญ๏ธ ํจํค์ง๊ฐ ๋ค๋ฅธ ๋๋ช
์ ํด๋์ค๋ค์ ๋ถ๋ฌ์ฌ ๊ฒฝ์ฐ
sec06.chap02.pkg1.Child child1 = new sec06.chap02.pkg1.Child();
sec06.chap02.pkg2.Child child2 = new sec06.chap02.pkg2.Child();
|
1
2
3
4
5
6
7
| public static void main(String[] args) {
//์ธ๋ถ์์ args๋ฐฐ์ด ๊ฐ ๋ฐ์์ค๊ธฐ
String menuName = args[0];
String spicyLevel = args[1];
System.out.printf("%s ๋งต๊ธฐ ๊ฐ๋ %s๋ก ์ฃผ๋ฌธ%n", menuName, spicyLevel);
}
|