목차
Static 변수
클래스 변수라고 불리는 이유
공통으로 사용하는 변수가 필요한 경우
번호 뽑는 기계 만들기
고유 카드 번호 만들기
Static 변수
Static 변수는 프로그래밍에서 중요한 개념 중 하나이다. 클래스 변수라고도 불리며, 클래스의 모든 인스턴스가 공유할 수 있는 변수이다. 즉 객체가 동일한 static 변수의 값을 공유한다.
클래스 변수라고 불리는 이유
우리가 자바 프로그램을 실행하면 프로그램을 수행하기 위해 운영체제로부터 할당받는 메모리들이 존재한다. 그 특성에 따라 영역이 존재한다.
실행 버튼(프로그램 실행)을 누르면 OS(운영체제)로부터 메모리 영역을 할당 받는다.
1. Method Area(Static) → 2. Statck(main) → 3. Heap(객체) 순서로 메모리 영역을 할당 받는다.
객체마다 각각 존재하는 멤버 변수를 사용하려면 이 값들이 메모리에 올라가야 사용할 수 있다.
static 변수는 '클래스명' + '.(점)' + '멤버변수'로 접근할 수 있다.
public class Test{
public int i; //멤버 변수
public Test(int i){
this.i = i;
}
//main함수 시작
public static main(String[] args){
Test test1 = new Test(1);
System.out.println(test1.i);
}//end of main
}//end of class
public class Test{
public static int i; //static 추가
public Test(int i){
this.i = i;
}
//main함수 시작
public static main(String[] args){
//'클래스명 + .(점) + 멤버변수명'으로 static 설정한 멤버 변수 호출
System.out.println(Test.i);
}//end of main
}//end of class
공통으로 사용하는 변수가 필요한 경우
■ 여러 인스턴스가 공유하는 기준 값이 필요할 때
■ 학생마다 새로운 학번 생성할 때
■ 카드 회사에서 카드를 새로 발급할 때마다 새로운 카드 번호를 부여할 때
■ 회사에 사원이 입사할 때마다 새로운 사번이 필요할 때
■ 은행에서 대기표를 뽑을 때 (단 번호표 기계가 2개 이상)
번호 뽑는 기계 만들기
[조건]
☞ 번호 뽑는 기계는 2대이다.
☞ 혼란을 방지하기 위해 같은 숫자가 출력되지는 않는다.
더보기
package basic.ch12;
public class NumberPrinter {
private int id;
public static int waitNumber;
public NumberPrinter(int id) {
this.id = id;
waitNumber = 1;
}
public void printWaitNumber() {
System.out.println(id + "번 기기의 대기 순번은 " + waitNumber);
waitNumber++;
}
}
package basic.ch12;
//번호 뽑아주는 기계
public class NumberPrinterTest {
public static void main(String[] args) {
NumberPrinter n1 = new NumberPrinter(1); // 왼쪽 기기
NumberPrinter n2 = new NumberPrinter(2); // 오른쪽 기기
n1.printWaitNumber(); // 고객1
n1.printWaitNumber(); // 고객2
n1.printWaitNumber(); // 고객3
n1.printWaitNumber(); // 고객4
n2.printWaitNumber(); // 고객5
n2.printWaitNumber(); // 고객6
}
}
더보기
private int id; //--> 기계 개수 / 멤버변수
public static int waitNumber; //--> 대기번호 / 멤버변수
//static 변수 --> Method Area 영역에 올라간다.
//즉 static waitNumber 변수는 NumberPrinter 인스턴스화되기 전에 사용가능하다.
//사용자 정의 생성자
public NumberPrinter(int id){
this.id = id; // 외부에서 입력한 int id 값이 곧 기계 번호이다.
waitNumber = 1; //대기 순번은 1번부터 시작한다.
}
//메서드(기능) --> 한 번 출력할 때마다 다음 번호를 위해 미리 순서 1을 더한다.
public void printWaitNumber(){
System.out.println(id + "번 기기의 대기 순번은 " + waitNumber);
waitNumber++; //증감 연산자 위치가 중요하다.
}
회사원 번호 만들기
[조건]
☞ 사원이 들어올 때마다 고유한 사원 번호를 할당하고 싶다.
☞ 단 사원 번호는 중복 되어서는 안된다.
더보기
public class Company {
public static String companyName = "\n과자 회사";
static int empSerialNumber = 1000;
}
package basic.ch12;
public class Employee {
private String name;
private String department;
private int myId;
public Employee(String name, String department) {
this.name = name;
this.department = department;
myId = Company.empSerialNumber;
System.out.println(Company.companyName);
System.out.println("이름 : " + this.name);
System.out.println("부서 : " + this.department);
System.out.println("고유사원번호 : " + myId);
Company.empSerialNumber++;
}
}
package basic.ch12;
public class CompanyTest {
public static void main(String[] args) {
Employee emp1 = new Employee("나비", "00부서");
Employee emp2 = new Employee("목련", "XX부서");
Employee emp3 = new Employee("구름", "XX부서");
Employee emp4 = new Employee("나무", "00부서");
Employee emp5 = new Employee("개울", "00부서");
}
}
'Java' 카테고리의 다른 글
Inheritance & Override (상속과 오버라이드) (0) | 2024.04.23 |
---|---|
Method Overloading 메서드 오버로딩 (0) | 2024.04.23 |
Constructor & Overloaded Constructor (생성자와 생성자 오버로딩) (1) | 2024.04.18 |
this를 활용하는 3가지 방법 (0) | 2024.04.18 |
접근 제어 지시자 (0) | 2024.04.18 |