시작하며
여러분 안녕하세요!
DreamHanks의254cm입니다.
이번 시간에는 static에 대해 설명하겠습니다.
Java의 전체 기사는 여기를 클릭해 주세요.
이전 기사는 [Java 개발] 제10회 Array List에 대해서 배워봤습니다.
static
static이란?
static은 클래스 구성 요소에 부여할 수 있는 수식자로서 메서드와 필드에 부여할 수 있습니다.
부여된 필드 및 메서드는 프로그램이 실행될 때 메모리를 할당받아
프로그램이 종료될 때까지 지워지지 않고 유지됩니다. 그리고 그 값은 모든 인스턴스가 공유합니다.
static 필드
일반적인 필드는 인스턴스가 생성될 때마다 새로 생성되어 다양한 값을 가지는데,
static이 부여된 필드는 프로그램이 실행될 때 생성되며 모든 인스턴스가 공유합니다.
샘플
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class Student { public static int studentNum = 20000; public String studentName; } public class ExampleCalss { public static void main(String[] args) { Student harry = new Student(); Student tom = new Student(); Student mugi = new Student(); Student coco = new Student(); System.out.println(harry.studentNum); System.out.println(tom.studentNum); System.out.println(mugi.studentNum); System.out.println(coco.studentNum); } } |
실행 결과
1 2 3 4 |
20000 20000 20000 20000 |
위의 샘플에서는 Student 클래스의 인스턴스를 생성하고 각 인스턴스의 student Num 값을 출력하고 있습니다.
각 인스턴스의 studentNum 아무것도 대입하지 않지만 두 다 20000가 출력되었습니다.
즉, 모든 인스턴스는 static 필드의 값을 공유하고 있습니다.
샘플
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class ExampleCalss { public static void main(String[] args) { Student harry = new Student(); Student tom = new Student(); Student mugi = new Student(); Student coco = new Student(); System.out.println(harry.studentNum++); System.out.println(tom.studentNum++); System.out.println(mugi.studentNum++); System.out.println(++coco.studentNum); } } |
실행 결과
1 2 3 4 |
20000 20001 20002 20004 |
모든 인스턴스가 static 필드를 공유하므로, 모든 인스턴스에서
실시한 처리 결과도 공유됩니다.
샘플 실행 결과를 보시면 Student의 모든 인스턴스는 동일한 student Num을 가리키고 있음을 알 수 있습니다.
※static 변수의 메모리는 다른 변수와 다른 영역에 저장됩니다.그 영역을 method영역이라고 부릅니다.
static 필드는 인스턴스와 다른 시점에 생성되므로 인스턴스가 아닌 클래스 이름으로 참조할 수 있습니다.
샘플
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class ExampleCalss { public static void main(String[] args) { Student harry = new Student(); Student tom = new Student(); Student mugi = new Student(); Student coco = new Student(); System.out.println(harry.studentNum++); System.out.println(tom.studentNum++); System.out.println(mugi.studentNum++); System.out.println(++coco.studentNum); System.out.println(Student.studentNum); } } |
실행 결과
1 2 3 4 5 |
20000 20001 20002 20004 20004 |
위와 같이 인스턴스를 사용하든 사용하지 않든 static 변수를 호출할 수 있습니다.
static 메서드
static 수식자는 클래스의 구성 요소에 붙이기 때문에 메소드에도 static 수식자를 붙일 수 있습니다.
이런 메서드를 static 메서드라고 부릅니다.static 메서드는 static 필드와 로컬 변수밖에 사용할 수 없습니다.
샘플
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class Student { public static int studentNum = 20000; public String studentName; public static void displayStudentNum() { System.out.println("지금 Student Num은" + studentNum + "입니다."); } } public class ExampleCalss { public static void main(String[] args) { Student.displayStudentNum(); Student.studentNum++; Student.displayStudentNum(); } } |
실행 결과
1 2 |
지금 Student Num은 20000입니다. 지금 Student Num은 20001입니다. |
위와 같이 인스턴스를 생성하지 않고도 클래스 이름을 참조하여 호출할 수 있습니다.
static 메서드에서 사용할 수 있는 변수
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Student { public static int studentNum = 20000; public String studentName; public int grade; public static void displayStudentNum() { int localVariable = 100; grade = 10; System.out.println("지금 Student Num은 "+ student Num + " 입니다."); } } |
static은 앞에서 설명한 것처럼 로컬 변수와 static 필드밖에 사용할 수 없습니다.
static 메서드는 인스턴스를 생성하지 않고도 사용할 수 있는 메서드입니다.
그래서, 일반 필드는 인스턴스가 생성될 때 생성되므로,
static 메서드에서는 사용할 수 없습니다.
・로컬 변수란?
괄호「{}」안에서 선언이 되고 선언된 괄호 안에서만 사용할 수 있는 변수입니다.
로컬변수는 선언된 영역이 종료되면 없어지는 변수입니다.
끝으로
이번 기사는 이상입니다.
다음 시간에는 자바의 final을 배워보겠습니다.
방문해 주셔서 감사합니다.
コメント