๋ฐ์ํ
โถ ํด๋์ค ๋ฉ์๋
static ๋ณ์๋ฅผ ์ํ ๋ฉ์๋๋ฅผ 'static ๋ฉ์๋' ๋๋ 'ํด๋์ค ๋ฉ์๋' ๋ผ๊ณ ํ๋ค.
package staticex;
public class Student2 {
private static int serialNum = 1000;
int studentID;
String studentName;
int grade;
String address;
public Student2() {
serialNum++;
studentID = serialNum;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String name) {
studentName = name;
}
public static int getSerialNum() { // serialNum์ get() ๋ฉ์๋
int i = 10;
return serialNum;
}
public static void setSerialNum(int serialNum) { // serialNum์ set() ๋ฉ์๋
Student2.serialNum = serialNum;
}
}
-์ฌ๊ธฐ์ this ์์ฝ์ด ์ฌ์ฉ ๋ถ๊ฐ๋ฅ.
โ
package staticex;
public class StudentTest4 {
public static void main(String[] args) {
Student2 studentLee = new Student2();
studentLee.setStudentName("์ด์ง์");
System.out.println(Student2.getSerialNum()); // serialNum ๊ฐ ๊ฐ์ ธ์ค๊ธฐ ์ํด get ๋ฉ์๋๋ฅผ ํด๋์ค ์ด๋ฆ์ผ๋ก ์ง์ ํธ์ถ
System.out.println(studentLee.studentName + " ํ๋ฒ:" + studentLee.studentID);
Student2 studentSon = new Student2();
studentSon.setStudentName("์์๊ฒฝ");
System.out.println(Student2.getSerialNum()); // serialNum ๊ฐ ๊ฐ์ ธ์ค๊ธฐ ์ํด get ๋ฉ์๋๋ฅผ ํด๋์ค ์ด๋ฆ์ผ๋ก ์ง์ ํธ์ถ
System.out.println(studentSon.studentName + " ํ๋ฒ:" + studentSon.studentID);
}
}
<๊ฒฐ๊ณผ๊ฐ>
-static ๋ฉ์๋ ๋ํ static ๋ณ์์ฒ๋ผ ์ธ์คํด์ค ์ฐธ์กฐ ๋ณ์๊ฐ ์๋ ํด๋์ค ์ด๋ฆ์ผ๋ก ์ง์ ํธ์ถํ ์ ์๋ค.
์ค์ต) ํ์๋ง๋ค ๊ฐ๊ฐ ๋ค๋ฅธ ํ์ ์นด๋๊ฐ ๋ฐ๊ธ๋ฉ๋๋ค. ํ์ ์นด๋ ๋ฒํธ๋ ํ๋ฒ์ 100์ ๋ํ ๊ฐ์ ๋๋ค. Student3 ํด๋์ค๋ฅผ ๋ง๋ค์ด ํ์ ์นด๋ ๋ฒํธ ๋ฉค๋ฒ ๋ณ์๋ฅผ ์ถ๊ฐํ๊ณ , ํ์์ด ์์ฑ๋ ๋๋ง๋ค ํ์ ์นด๋ ๋ฒํธ๋ฅผ ๋ถ์ฌํฉ๋๋ค. StudentTest6 ํด๋์ค๋ฅผ ๋ง๋ค์ด ํ์ ๋ ๋ช ์ ์์ฑํฉ๋๋ค. ๋ ํ์์ ์นด๋ ๋ฒํธ๋ฅผ ์ถ๋ ฅํด ๋ณด์ธ์.
package staticex;
public class Student3 {
private static int serialNum = 1000;
int studentID;
String studentName;
int grade;
String address;
int studentCardNum;
public Student3() {
serialNum++;
studentID = serialNum;
studentCardNum = serialNum+100;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String name) {
studentName = name;
}
public static int getSerialNum() {
int i = 10;
return serialNum;
}
public static void setSerialNum(int serialNum) {
Student3.serialNum = serialNum;
}
public int getStudentCardNum() {
return studentCardNum;
}
public void setStudentCardNum(int studentCardNum) {
this.studentCardNum = studentCardNum;
}
}
package staticex;
public class StudentTest6 {
public static void main(String[] args) {
Student3 studentLee = new Student3();
studentLee.setStudentName("์ด์ง์");
System.out.println(Student3.getSerialNum());
System.out.println(studentLee.studentName + " ํ๋ฒ:" + studentLee.studentCardNum);
Student3 studentSon = new Student3();
studentSon.setStudentName("์์๊ฒฝ");
System.out.println(Student3.getSerialNum());
System.out.println(studentSon.studentName + " ํ๋ฒ:" + studentSon.studentCardNum);
}
}
<๊ฒฐ๊ณผ๊ฐ>
โ
๋ฐ์ํ
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] static ์์ฉ - ์ฑ๊ธํค ํจํด (0) | 2021.02.19 |
---|---|
[Java] ๋ณ์ ์ ํจ ๋ฒ์ (0) | 2021.02.19 |
[Java] Static ๋ณ์ (0) | 2021.02.18 |
[Java] ๊ฐ์ฒด ๊ฐ ํ๋ ฅ (0) | 2021.02.18 |
[Java] this ์์ฝ์ด (0) | 2021.02.18 |
๋๊ธ