본문 바로가기

백엔드개발자 준비하기

[백엔드개발자 준비하기] 스태틱(Static)

스태틱 안에는 스태틱만 사용가능

 

예시1

	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]

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;
	}

	public static int getAutoIncrement() {
    	System.out.println("현재 AI : " + ai);
		return ai;
	}
	
	@Override
	public String toString() {
		return "Student [studentCode=" + studentCode + ", name=" + name + "]";
	}
}

[Teacher]

public class Teacher {

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

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

[StudentMain]

public class StudentMain {

	public static void main(String[] args) {
		System.out.println(Student.getAutoIncrement());
		
		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();
		}
		int j = 0;
		for (Student s : students) {
			System.out.println(s);
			System.out.println(teachers[j]);
			j++;
			System.out.println();
		}
		
		System.out.println(Student.getAutoIncrement());
	}
}