๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java] ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 18.
๋ฐ˜์‘ํ˜•

โ–ถ ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ

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

๋Œ“๊ธ€