- static 키워드
-변수와 메서드에 사용 가능
- 인스턴스 생성 전, 클래스가 메모리에 로딩되는 시점에 함께 로딩됨
=> 인스턴스 생성과 무관하며, 모든 인스턴스에서 하나의 메모리를 공유함
- 인스턴스 주소를 갖는 참조변수 대신 클래스명만으로 접근 가능
static 변수 (= 클래스 변수 = 정적변수)
- 변수의 데이터타입 앞에 static 키워드를 사용
- 인스턴스 생성 없이 클래스가 메모리에 로딩될 때 변수도 함께 로딩됨
=> 클래스명만으로 접근 가능하며, 모든 인스턴스에서 하나의 변수를 공유함
<기본 사용 문법>
선언 : [접근제한자] static 데이터타입 병수명;
사용 : 클래스명.변수명
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
public class Ex {
public static void main(String[] args) {
StaticTest.a // 클래스명.변수명만으로 접근 가능
StaticTest.b // 오류발생 클래스명.변수명 만으로 접근 불가능
Student s = new Student();
System.out.println("학교이름 : " + s.schoolName); // 하나의 인스턴스 변수 값을 참조하기때문에 같음
Student s1 = new Student();
System.out.println("학교이름 : " + s1.schoolName); // 하나의 인스턴스 변수 값을 참조하기때문에 같음
}
}
class StaticTest{
static int a =10;
int b;
}
class Student{
static String schoolName = "부산 아이티윌";
String name;
int age;
}
|
cs |
- static 메서드(= 정적 메서드 = 클래스 메서드)
static 변수와 마찬가지로 클래스가 메모리에 로딩될 때 함께 로딩됨
(인스턴스 생성 전에 로딩됨)
-인스턴스 생성 없이 클래스명 만으로 호출 가능
-static 메서드 내에서는 인스턴스 변수 사용 불가
=> 인스턴스 변수가 메모리에 생성되기 전에 static 메서드가 로딩되기 때문
-static 메서드 내에서는 레퍼런스 this 사용불가
= > 레퍼런스 this 도 인스턴스 생성 시점에서 메모리에 로딩되기 때문
= > 만약, 로컬변수와 static 변수명이 같을경우
static 변수를 구별하기 위해 클래스명.static 변수명 형태로 지정
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
class StaticMethod{
static int staticMember;
int instanceMeber;
public static void staticMethod(){
System.out.println("staticMethod() 메서드 호출됨")
System.out.println("staticMember 변수값 : " + staticMember);
// System.out.println("instanceMember 변수값 : " + instanceMember); // 오류 발생!
// static 메서드 로딩 시점은 인스턴스 변수가 생성되기 전이므로
// static 메서드 내에서는 인스턴스 변수 접근이 불가능하다!
}
public void normaleMethod(){
System.out.println("normalMethod() 메서드 호출됨");
System.out.println("staticMember 변수값 : " + staticMember);
System.out.println("instanceMember 변수값 : " + instanceMember);
}
public void setInstanceMember(int instanceMember){
//인스턴스 변수와 로컬변수의 이름이 동일할 때 인스턴스변수를 구별하기위해
//인스턴스 변수명 앞에 레퍼런스 this 를 사용하여 this.인스턴스 변수명 형태로 지정
this.instanceMember = instanceMember;
}
public static void setStaticMember(int staticMember){
//static 메서드 내에서 레퍼런스 this 사용 불가능
// 레퍼런스 this 는 인스턴스 생성 시점에서 로딩되므로 static 메서드 로딩 시 존재x
// this.staticMember = staticMember; //오류발생
//Setter 등의 메서드 내의 로컬변수명과 static 변수명이 같을 경우
//static 변수를 지정하기 위해 클래스명.static변수명 사용
StaticMethod.staticMember = staticMember;
}
}
|
cs |
<프로그램 실행 과정 및 변수 생성 시점>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public class Ex3 {
int b = check(2); // 인스턴스 변수(new Ex3() = > 인스턴스 생성 시점에 로딩됨)
// => 정수 2 출력(4등)
static int a = check(1); // static 변수 = > 클래스 로딩 시점에 로딩됨
// => 이때, static 메서드 check() 를 호출하여 점수 1 전달하므로
// 정수 1 출력(1등)
public static int check(int i) { // static 메서드
System.out.println("call " + i);
return 0;
}
public static void main(String[] args) { // 클래스 로딩 시점에 로딩됨
// static 멤버 로딩이 끝난 후 main() 메서드가 호출됨
System.out.println("main() 메서드 호출됨"); // 3등
Ex3 ex = new Ex3(); // 인스턴스 생성
}
static int c = check(3); // static 변수 = > 클래스 로딩 시점에 로딩됨
// => 이때, static 메서드 check() 를 호출하여 점수 1 전달하므로
// 정수 3출력(2등)
}
|
cs |
< 자바 프로그램 실행 과정 및 변수 생성 시점 >
1. 소스 코드(.java) 작성 후 실행
2. 클래스 파일(.class) 생성
3. 클래스 파일 실행
4. 클래스 로딩 = > static 키워드 사용된 변수 및 메서드 로딩됨
5. main() 메서드 호출(실행)
6. 인스턴스 생성 = > 인스턴스 변수 및 메서드 로딩됨
7. 메서드 호출 = > 로컬 변수 로딩됨
8. 메서드 종료 = > 로컬 변수 제거됨
9. 인스턴스 제거됨 => 인스턴스 변수 제거됨
10. 프로그램 종료됨 => static 변수 제거됨
'Language > Java' 카테고리의 다른 글
상속(Inheritance) (0) | 2020.06.04 |
---|---|
Singleton Design Pattern(싱글톤 디자인 패턴) (0) | 2020.06.01 |
this (0) | 2020.05.28 |
파라미터 생성자 (0) | 2020.05.28 |
생성자(Constructor) (0) | 2020.05.28 |