본문 바로가기
Programming Languages/Java

Chapter 21. 스태틱(static)

by 더 이프 2023. 1. 13.
728x90

 

1. 스태틱(static)

■ 스태틱이란?

  • 스태틱은 공유되는 메모리 영역
  • 객체 생성 없이 사용 가능
  • 스태틱으로 쓴 변수는 누워있는 글씨체를 가짐
  • 스태틱 메소드는 호출하지 않아도 사용가능하기 때문에 생성해야지만 사용가능한 멤버 변수가 있으면 에러가 일어남
  • 스태틱 메소드 안에서는 스태틱 멤버 변수와 메소드 내의 지역 변수만 사용 가능

 

2. 예시

■ TestA

package j17_스태틱;

public class TestA {

	private static int num;
	
	public static void setNum(int num) {
		TestA.num = num;
	}
	
	public static int getNum() {
		return num;
	}
}

■ StaticMain

package j17_스태틱;

public class StaticMain {

	public static void main(String[] args) {
		
		System.out.println("출력1 : " + TestA.getNum());
		System.out.println();
		
		TestA.setNum(100);
		System.out.println("출력2 : " + TestA.getNum());
	}
}

■ Student

package j17_스태틱;

public class Student {
	
	private static final int CODE = 20230000;
	private static int ai = 1; // auto_increment
	
	private int studentCode;
	private String name;
	
	public Student(String name) {
		studentCode = CODE + ai;
		ai++;
		this.name = name;
	}

	// 호출하지 않아도 사용가능한 static메소드에 생성해야지만 사용가능한 변수가 있기 때문에 에러가 일어남
	// static 메소드 안에서는 static 멤버 변수와 지역 변수만 가능
	public static int getAutoIncrement() {
		System.out.println("현재 AI : " + ai);
        
        // name은 static 변수가 아니라 불가
//		System.out.println("학생이름 : " + name); 
		return ai;
	}
	
	@Override
	public String toString() {
		return "Student [studentCode=" + studentCode + ", name=" + name + "]";
	}
	
}

■ Teacher

package j17_스태틱;

public class Teacher {

	private String name;

	public Teacher(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Teacher [name=" + name + "]";
	}
}

■ StudentMain

package j17_스태틱;

public class StudentMain {

	public static void main(String[] args) {

		Student[] students = new Student[5];
		Teacher[] teachers = new Teacher[5];

		students[0] = new Student("이현수");
		students[1] = new Student("김재영");
		students[2] = new Student("이상현");
		students[3] = new Student("박성진");
		students[4] = new Student("윤선영");

		teachers[0] = new Teacher("김준일1");
		teachers[1] = new Teacher("김준일2");
		teachers[2] = new Teacher("김준일3");
		teachers[3] = new Teacher("김준일4");
		teachers[4] = new Teacher("김준일5");

		for (int i = 0; i < students.length; i++) {
			System.out.println(students[i]);
			System.out.println(teachers[i]);
			System.out.println();
		}

		// 하나의 배열을 출력시 foreach가 사용하기 좋음
		int j = 0;
		for (Student student : students) {
			System.out.println(student);
			System.out.println(teachers[j]);
			System.out.println();
			j++;

		}

		System.out.println(Student.getAutoIncrement());
	}
}

■ Car

package j17_스태틱;

import java.time.LocalDateTime;

public class Car {

	// 현재 날짜 중 년도만 가져옴
	private final static int NOW_YEAR = LocalDateTime.now().getYear();

	private String serialCode; // KIA-2023-00
	private String modelName;
	private static final String CODE = "KIA-" + NOW_YEAR + "-";
	private static int ai = 1;

	public Car(String modelName) {
    	// 숫자 앞에 몇자리 수의 수 앞에 0을 채워줌
		serialCode = CODE + String.format("%04d", ai); 
		this.modelName = modelName;
		ai++;
	}

	@Override
	public String toString() {
		return "Car [serialCode=" + serialCode + ", modelName=" + modelName + "]";
	}
}

■ CarMain

package j17_스태틱;

public class CarMain {

	public static void main(String[] args) {
		Car[] cars = new Car[10];

		cars[0] = new Car("레이");
		cars[1] = new Car("스팅어");
		cars[2] = new Car("쏘렌토");
		cars[3] = new Car("스포티지");
		cars[4] = new Car("모닝");
		cars[5] = new Car("K3");
		cars[6] = new Car("K5");
		cars[7] = new Car("K7");
		cars[8] = new Car("모하비");
		cars[9] = new Car("카니발");

		for (Car car : cars) {
			System.out.println(car);
		}
	}
}