์ผ๋ฐ์ ์ผ๋ก Java ๋ฒ์
๋ณ์์ ๋ฉ์๋์ ๋ฒ์๋ฅผ ๋ถ๋ฆฌํด์ผ ํ๋ ์ด์ ๋ ๋ฌด์์ ๋๊น? ์ฌ์ค ๋๋๋ก ํ๋ก๊ทธ๋จ์ ๋งค์ฐ ํฌ๊ณ ํ๋ ๋๋ ๋ค๋ฅธ ๋ณ์๋ฅผ ์ถ์ ํ๊ธฐ ์ด๋ ค์ธ ์ ์์ต๋๋ค. ๋ํ ๋๊ท๋ชจ ํ๋ก์ ํธ์์๋ ๋ณ์์ ์ฉ๋๊ฐ ๋ช ํํ๋๋ก ๋ณ์ ์ด๋ฆ์ ๋ช ํํ๊ฒ ์ง์ ํ๋ ๊ฒ์ด ๋ฐ๋์งํฉ๋๋ค. ๋ฒ์๋ ํนํ ํ๋ก๊ทธ๋จ์ ๋ค๋ฅธ ๋ถ๋ถ์์ ๋์ผํ ์ด๋ฆ์ ๊ฐ์ง ๋ค๋ฅธ ๋ณ์๋ฅผ ๊ฐ์ง ์ ์๋๋ก ํฉ๋๋ค. ์ด๋ฌํ ์ฝ๋๋ ์ ์ง ๊ด๋ฆฌ ๋ฐ ์ฝ๊ธฐ๊ฐ ๋ ์ฝ์ต๋๋ค. Java ๋ฒ์๋ ํ๋ก๊ทธ๋จ์์ ํน์ ๋ณ์ ๋๋ ๋ฉ์๋์ ์ก์ธ์คํ ์ ์๋ ์์น๋ฅผ ์ ์ํฉ๋๋ค. ๊ฐ๋จํ:- ๋ฉ์๋์์ ์ ์ธ๋ ๋ณ์๋ ์ ์ธ ์์๋ถํฐ ๋ฉ์๋ ๋๊น์ง(๋ฉ์๋ ๋ฒ์) ํ์๋ฉ๋๋ค.
- ์ฝ๋ ๋ธ๋ก์์ ์ ์ธ๋ ๋ณ์๋ ํด๋น ์ฝ๋ ๋ธ๋ก์ด ๋๋ ๋๊น์ง ์กด์ฌํฉ๋๋ค.
- ๋ฉ์๋ ์ธ์์ธ ๋ณ์๋ ๋ฉ์๋๊ฐ ๋๋ ๋๊น์ง ์กด์ฌํฉ๋๋ค.
- ํด๋์ค/๊ฐ์ฒด ๋ณ์๋ ํฌํจ ๊ฐ์ฒด์ ์๋ช ๋์ ์กด์ฌํฉ๋๋ค. ๊ฐ์์ฑ์ ํน์ ์ก์ธ์ค ์์ ์์ ์ํด ๊ท์ ๋ฉ๋๋ค.
- ์ ์ ํด๋์ค ๋ณ์๋ ํ๋ก๊ทธ๋จ์ด ์คํ๋๋ ๋์ ํญ์ ์กด์ฌํฉ๋๋ค. ๊ฐ์์ฑ๋ ์ก์ธ์ค ํ์ ์์ ์ํด ๊ฒฐ์ ๋ฉ๋๋ค.
๋ฐฉ๋ฒ ์์ค ๋ฒ์
์ธ์๋ฅผ ํฌํจํ์ฌ ๋ฉ์๋์์ ์ ์ธ๋ ๋ชจ๋ ๋ณ์๋ ํด๋น ๋ฉ์๋ ์ธ๋ถ์์ ์ก์ธ์คํ ์ ์์ต๋๋ค. ๋ฉ์๋ ๋ด์์ ์ ์ธ๋ ๋ชจ๋ ๋ณ์๋ ์ ์ธ ์์๋ถํฐ ๋ฉ์๋ ๋๊น์ง ๋ณผ ์ ์์ต๋๋ค. ๋ค์์ ๋ฉ์๋ ๋ณ์ ๋ฒ์์ ์์ ๋๋ค.
public class JScopeTest1 {
public static void main(String[] args) {
System.out.println(myMethod(5));
System.out.println(myMethod(17));
}
public static int myMethod(int arg) {
int secondArg = 100; //local method variable
return secondArg + arg;
}
}
์ฌ๊ธฐ์ ์ง์ญ ๋ณ์ ๋๋ ๋ฉ์๋ ์ธ์์ธ secondArg ๊ฐ ์์ต๋๋ค . myMethod ๋ฉ์๋ ์ธ๋ถ์์ ๋๋ ์ ์ธ๋๊ธฐ ์ ์๋ ์ด ๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค . ๋ณ์๊ฐ ํจ์ ์ธ์์ธ ๊ฒฝ์ฐ ์ด ๋ฉ์๋์ ์ ์ฒด ๋ณธ๋ฌธ์์ ๋ณผ ์ ์์ต๋๋ค. ์์ ์์๋ myMethod์ arg์ ๊ธฐ๋ณธ ๋ฉ์๋์ args๋ผ๋ ๋ ๊ฐ์ง ์ธ์๊ฐ ์์ต๋๋ค.
ํด๋์ค ์์ค ๋ฒ์
ํด๋์ค ์์ค ๋ฒ์(์ธ์คํด์ค ๋ณ์) โ ํด๋์ค์์ ์ ์ธ๋ ๋ชจ๋ ๋ณ์๋ ํด๋น ํด๋์ค์ ๋ชจ๋ ๋ฉ์๋์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ก์ธ์ค ํ์ ์(์: public ๋๋ private)์ ๋ฐ๋ผ ํด๋์ค ์ธ๋ถ์์ ์ก์ธ์คํ ์ ์๋ ๊ฒฝ์ฐ๊ฐ ์์ต๋๋ค. ๋ฐ๋ผ์ ๋ณ์๊ฐ ํด๋์ค ๋ณ์๋ผ๋ฉด ํน์ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ๋์ด ์ด ํด๋์ค์ ๊ฐ์ฒด๊ฐ ์๋ ํ ์กด์ฌํฉ๋๋ค. ๊ฐ์ฒด๊ฐ ์์ผ๋ฉด ๋ณ์์ ๋ณต์ฌ๋ณธ์ด ์์ต๋๋ค. ๋ณ์๋ ๋ณ์๊ฐ ์ ์ธ๋๊ธฐ ์ ์ด๋ ํ์ ์ ์ธ๋์๋์ง ์ฌ๋ถ์ ๊ด๊ณ์์ด ํด๋์ค์ ๋ชจ๋ ๋ฉ์๋์์ ๋ณผ ์ ์์ต๋๋ค. ๊ฐ ๊ฐ์ฒด์๋ ๋ค๋ฅธ ๊ฐ์ฒด์ ๋ ๋ฆฝ์ ์ธ ์์ฒด ๋ณ์๊ฐ ์์ต๋๋ค. ์ ์ ๋ฉ์๋์์ ๋ณ์์ ์ก์ธ์คํ ์ ์์ต๋๋ค.์ฝ๋ ์
public class Student {
//class level variables
public String surname;
String name;
String secondName;
private Long birthday; // Long instead of long is used by Gson/Jackson json parsers and various orm databases
public Student(String surname, String name, String secondName, Date birthday ){
this.surname = surname;
this.name = name;
this.secondName = secondName;
this.birthday = birthday == null ? 0 : birthday.getTime();
}
@Override
public int hashCode(){
//TODO: check for nulls
//return surname.hashCode() ^ name.hashCode() ^ secondName.hashCode() ^ (birthday.hashCode());
return (surname + name + secondName + birthday).hashCode();
}
@Override
public boolean equals(Object other_) {
Student other = (Student)other_;
return (surname == null || surname.equals(other.surname) )
&& (name == null || name.equals(other.name))
&& (secondName == null || secondName.equals(other.secondName))
&& (birthday == null || birthday.equals(other.birthday));
}
}
Surname , name , secondName ๋ฐ birthday ๋ ์ธ์คํด์ค ๋ณ์์
๋๋ค.
๋ธ๋ก ๋ฒ์
๋ณ์๊ฐ ์ผ๋ถ ์ฝ๋ ๋ธ๋ก์์ ์ ์/์ ์ธ๋๋ฉด ํด๋น ์ฝ๋ ๋ธ๋ก์ด ๋๋ ๋๊น์ง ์กด์ฌํฉ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก ์ด๋ฌํ ๋ณ์๋ ์ ์๋ ์ค๊ดํธ ์ฌ์ด์ ์กด์ฌํฉ๋๋ค. ๋งค์ฐ ์์ฃผ ๋ธ๋ก ๋ฒ์๋ ๋ฃจํ ๋ณ์๊ฐ ๋ ์ ์์ต๋๋ค. for ๋ฃจํ ์กฐ๊ฑด ์์ ์ ์ธ๋ ๋ณ์๋ ๋ฏธ๋ฆฌ ์ ์ํ์ง ์๋ ํ ๋ฃจํ ์ธ๋ถ์์ ์ก์ธ์คํ ์ ์์ต๋๋ค.
public class JScopeTest2 {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
int sum = 0;
sum = sum + i;
}
int sum = 1;
System.out.println(sum);
}
}
์ฒซ ๋ฒ์งธ ํฉ๊ณ์ i ๋ณ์๋ ๋ชจ๋ ๋ฃจํ ๋ด๋ถ์์ ์ ์ธ๋๋ฉฐ ์ด ๋ฃจํ ์ธ๋ถ์๋ ์กด์ฌํ์ง ์์ต๋๋ค. ๊ทธ๋ฌ๋ ๋ ๋ฒ์งธ ํฉ๊ณ๋ ๋ฃจํ ์ธ๋ถ์์ ์ ์ธ๋์์ผ๋ฏ๋ก ์ด ํน์ ๋ณ์๊ฐ ์ธ์๋ ๊ฒ์
๋๋ค.
์ ์ ๋ณ์
๋ณ์๊ฐ ์ ์ (static ํค์๋๋ก ํ์๋จ)์ผ๋ก ์ ์ธ๋๋ฉด ํด๋น ํด๋์ค๊ฐ ์กด์ฌํ๋ ํ ์กด์ฌํฉ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก JVM์ ์ ์ ๋ณ์๊ฐ ์ด๊ธฐํ๋ ๋ ์ฒ์ ์ฌ์ฉํ ๋ ํด๋์ค๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋ํฉ๋๋ค.
import java.util.Date;
public class Student {
public static int today = 2022;
String surname;
String name;
String secondName;
Long birthday; // Long instead of long is used by Gson/Jackson json parsers and various orm databases
public Student(String surname, String name, String secondName, Date birthday ){
this.surname = surname;
this.name = name;
this.secondName = secondName;
this.birthday = birthday == null ? 0 : birthday.getTime();
}
public static void main(String[] args) {
System.out.println(today);
}
}
์ ์ today ๋ณ์๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด Student ํด๋์ค ์ ์ ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ฉด ์ ๋ฉ๋๋ค . ์ฌ๊ธฐ์ "2022"๊ฐ ์ธ์๋ฉ๋๋ค.
์ก์ธ์ค ํ์ ์
Java์๋ ๋ฉ์๋ ๋๋ ๋ณ์์ ๋ํ ์ก์ธ์ค๋ฅผ ์ ํํ๋ 4๊ฐ์ ์ก์ธ์ค ํ์ ์๊ฐ ์์ต๋๋ค. ๋ฉ์๋ ๋ด๋ถ๊ฐ ์๋ ํด๋์ค ๋ด๋ถ์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.-
private์ ๊ฐ์ฅ ์ ํ์ ์ธ ํ์ ์์ ๋๋ค. ๋ฉ์๋ ๋ฐ ๋ณ์์ ๋ํ ์ก์ธ์ค๋ฅผ ์ ์ธ๋ ํด๋์ค๋ก ์ ํํฉ๋๋ค. ํด๋์ค ์ธ๋ถ์์ ํน์ ๋ฉ์๋๋ ๋ณ์๋ฅผ ์ฌ์ฉํ ํ์๊ฐ ์๋ค๋ฉด private์ ์ฌ์ฉํ์ญ์์ค. ํด๋์ค ๋ณ์๋ ์ผ๋ฐ์ ์ผ๋ก Java์์ ๋น๊ณต๊ฐ์ ๋๋ค.
-
์ก์ธ์ค ํ์ ์๊ฐ ์ง์ ๋์ง ์์ ๊ฒฝ์ฐ ๋ฉ์๋ ๋๋ ๋ณ์๋ ๊ธฐ๋ณธ ํ์ ์๋ฅผ ํ์ฉํฉ๋๋ค. ๊ธฐ๋ณธ๊ฐ์ ํ์ฌ ํจํค์ง์์๋ง ์ก์ธ์ค๋ฅผ ํ์ฉํฉ๋๋ค.
-
protected ์์ ์๋ ํจํค์ง ์ธ๋ถ์ ์์ ํด๋์ค๋ฅผ ํตํด ์ก์ธ์คํ์ง ์๋ ํ ํ์ฌ ํจํค์ง ๋ด์์๋ง ๋ฉ์๋ ๋๋ ๋ณ์์ ๋ํ ์ก์ธ์ค๋ฅผ ํ์ฉํฉ๋๋ค.
-
public ์ ์ ํ์ด ๊ฐ์ฅ ์ ์ ํ์ ์์ ๋๋ค. ์ ์ธ๋ ํด๋์ค๋ฟ๋ง ์๋๋ผ ์ธ๋ถ์์๋ ํด๋์ค, ๋ฉ์๋ ๋๋ ๋ณ์์ ์ก์ธ์คํ ์ ์์ต๋๋ค. ์ด ์์ ์๋ ์ ๋ง ์์ฃผ ์ฌ์ฉ๋ฉ๋๋ค.
GO TO FULL VERSION