본문 바로가기
자격증/정보처리기사

[JAVA/자바] 프로그래밍 - static

by 행복한라이언 2023. 10. 1.
728x90
반응형

참고

https://blog.naver.com/heartflow89/220959033435

 

[JAVA/자바] 정적 멤버(필드, 메소드)와 static

 이전 포스팅은 인스턴스 멤버에 대해서 알아보았고 반드시 객체를 통해서만 접근이 가능했다. 그러면...

blog.naver.com


핵심

1. static field, static method는 인스턴스 생성하지 않아도 된다. 클래스명 바로 사용 가능

public class Main{
	public static void main(String[] args){
		// static 메서드, static 필드는 인스턴스 생성하지 않아도 된다.
		System.out.println(A.staticField); // 5
		A.staticField = 1;
		System.out.println(A.staticField); // 1
		A.staticPrint("hello"); // A_staticPrint : hello

 

2. static field, statice method가 아닌 경우는 인스턴스 생성해야 쓸 수 있다.

public class Main{
	public static void main(String[] args){
    	//..
        //인스턴스 필드, 메서드는 인스턴스 생성해야함.
		A aClass = new A(); // 생성자 존재 "클래스 A 생성자 호출!"
		System.out.println(aClass.instanceFiled); // 1
		aClass.instancePrint("byebye"); // B_instancePrint : byebye
	}
}

 

3. static method는 오버라이딩 불가

→ instanPrint는 오버라이딩해 "B _ instancePrint : Middle" 이 출력

→ staticPrint는 오버라딩이 되지 않아 "A _ staticPrint : Middle" 이 출력 

→ 따라서 static 메서드는 상속받아도 오버라이딩은 할 수 없다.

class B extends A{

	void instancePrint(String msg){
		System.out.println("B _ instancePrint : " + msg);
	}

	static void staticPrint(String msg){
		System.out.println("B _ staticPrint : " + msg);
	}
}

//..

A bClass = new B();
bClass.instancePrint("Middle"); // B _ instancePrint : Middle
bClass.staticPrint("Middle"); // A _ staticPrint : Middle

 

4. static method this 사용 불가

public class MyClass {
    private int value;

    public MyClass(int value) {
        this.value = value;
    }

    public static void printValue() {
        System.out.println(this.value); // 컴파일 에러 발생
    }

    public static void main(String[] args) {
        MyClass obj = new MyClass(42);
        obj.printValue();
    }
}

예제코드

class A{
	A(){
		System.out.println("클래스 A 생성자 호출!");
	}
	//인스턴스 필드
	int instanceFiled = 1;
    // statice 필드
	static int staticField = 5;

	void instancePrint(String msg){
		System.out.println("A _ instancePrint : " + msg);
	}

	static void staticPrint(String msg){
		System.out.println("A _ staticPrint : " + msg);
	}
}

class B extends A{

	void instancePrint(String msg){
		System.out.println("B _ instancePrint : " + msg);
	}

	static void staticPrint(String msg){
		System.out.println("B _ staticPrint : " + msg);
	}
}

public class Main{
	public static void main(String[] args){
		// static 메서드, static 필드는 인스턴스 생성하지 않아도 된다.
		System.out.println(A.staticField); // 5
		A.staticField = 1;
		System.out.println(A.staticField); // 1

		A.staticPrint("hello"); // staticPrint : hello
		
        //인스턴스 필드, 메서드는 인스턴스 생성해야함.
		A aClass = new A(); // 생성자 존재 "클래스 A 생성자 호출!"
		System.out.println(aClass.instanceFiled); // 1
		aClass.instancePrint("byebye"); // instancePrint : byebye

		A bClass = new B();
		bClass.instancePrint("Middle"); // B _ instancePrint : Middle
		bClass.staticPrint("Middle"); // A _ staticPrint : Middle 

	}
}

// 출력
5
1
A _ staticPrint : hello
클래스 A 생성자 호출!
1
A _ instancePrint : byebye
클래스 A 생성자 호출!
B _ instancePrint : Middle
A _ staticPrint : Middle

 

 

728x90
반응형