๋ฐ์ํ
โถ Static ๋ณ์
package staticex;
public class Student {
public static int serialNum = 1000; // static ๋ณ์๋ ์ธ์คํด์ค ์์ฑ๊ณผ ์๊ด์์ด ๋จผ์ ์์ฑ๋จ
public int studentID;
public String studentName;
public int grade;
public String address;
public String getStudentName() {
return studentName;
}
public void setStudentName(String name) {
studentName = name;
}
}
package staticex;
public class StudentTest1 {
public static void main(String[] args) {
Student studentLee = new Student();
studentLee.setStudentName("์ด์ง์");
System.out.println(studentLee.serialNum); // ์ด๊น๊ฐ ์ถ๋ ฅ
studentLee.serialNum++; // static ๋ณ์ ์ฆ๊ฐ
Student studentSon = new Student();
studentSon.setStudentName("์์๊ฒฝ");
System.out.println(studentSon.serialNum); // ์ฆ๊ฐ๋ ๊ฐ ์ถ๋ ฅ
System.out.println(studentLee.serialNum); // ์ฆ๊ฐ๋ ๊ฐ ์ถ๋ ฅ
}
}
<๊ฒฐ๊ณผ๊ฐ>
โ
-ํ๋ฒ ์์ฑํ๊ธฐ
package staticex;
public class Student1 {
public static int serialNum = 1000;
public int studentID;
public String studentName;
public int grade;
public String address;
public Student1() {
serialNum++; // ํ์์ด ์์ฑ๋ ๋๋ง๋ค ์ฆ๊ฐ
studentID = serialNum; // ์ฆ๊ฐ๋ ๊ฐ์ ํ๋ฒ ์ธ์คํด์ค ๋ณ์์ ๋ถ์ฌ
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String name) {
studentName = name;
}
}
package staticex;
public class StudentTest2 {
public static void main(String[] args) {
Student1 studentLee = new Student1();
studentLee.setStudentName("์ด์ง์");
System.out.println(studentLee.serialNum);
System.out.println(studentLee.studentName + " ํ๋ฒ:" + studentLee.studentID);
Student1 studentSon = new Student1();
studentSon.setStudentName("์์๊ฒฝ");
System.out.println(studentSon.serialNum);
System.out.println(studentSon.studentName + " ํ๋ฒ:" + studentSon.studentID);
}
}
package staticex;
public class StudentTest3 {
public static void main(String[] args) {
Student1 studentLee = new Student1();
studentLee.setStudentName("์ด์ง์");
System.out.println(Student1.serialNum); // serialNum ๋ณ์๋ฅผ ์ง์ ํด๋์ค ์ด๋ฆ์ผ๋ก ์ฐธ์กฐ
System.out.println(studentLee.studentName + " ํ๋ฒ:" + studentLee.studentID);
Student1 studentSon = new Student1();
studentSon.setStudentName("์์๊ฒฝ");
System.out.println(Student1.serialNum); // serialNum ๋ณ์๋ฅผ ์ง์ ํด๋์ค ์ด๋ฆ์ผ๋ก ์ฐธ์กฐ
System.out.println(studentSon.studentName + " ํ๋ฒ:" + studentSon.studentID);
}
}
ํด๋์ค. ํด๋์ค ๋ณ์ โ
์ธ์คํด์ค. ํด๋์ค ๋ณ์ โ
ํด๋์ค. ์ธ์คํด์ค ๋ณ์ X
๋ฐ์ํ
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ๋ณ์ ์ ํจ ๋ฒ์ (0) | 2021.02.19 |
---|---|
[Java] ํด๋์ค ๋ฉ์๋ (0) | 2021.02.18 |
[Java] ๊ฐ์ฒด ๊ฐ ํ๋ ฅ (0) | 2021.02.18 |
[Java] this ์์ฝ์ด (0) | 2021.02.18 |
[Java] ์ฐ์ต๋ฌธ์ + ์ค์ต (0) | 2021.02.18 |
๋๊ธ