blues_log
Published 2023. 6. 9. 10:55
[Java] static Java/Java 정리

static 

public static void main(String[] args) {

}

메소드를 선언할 때 나오는 static은 어떤 경우에는 붙이고 어떤 경우에는 붙이지 않는 경우가 있다.

결과부터 정리하면 다음과 같다.

  • class method에서는 static을 붙인다.
  • instance method에서는 static을 붙이지 않는다.

다음 코드의 예에서 그 차이를 명확히 알 수 있을 것이다.

Class Blue {
	public void a() {
    	System.out.println("A")
    }
}

public class InstanceMethod {

	public static void main(String[] args) {
    	
        Blue b = new Blue();
        b.a()
    }
}

a()라는 메소드는 밑에서 instance method로 활용되었다. 그래서 a() 메소드를 만들 때 static을 붙이지 않는다.

 

 

Class Blue {
	public static void a() {
    	System.out.println("A")
    }
}

public class InstanceMethod {

	public static void main(String[] args) {
    	Blue.a();
    }
}

여기서 a() 메소드는 class method로 활용되었다. 그래서 a() 메소드를 만들 때 static을 붙인다.

 


참고

생활코딩 자바 https://opentutorials.org/module/4397/26913

'Java > Java 정리' 카테고리의 다른 글

[Java] 제네릭(Generic)  (1) 2023.06.10
[Java] 향상된 for 문  (0) 2023.06.08
[Java] 예외  (0) 2023.06.08
[Java] 배열(Array)  (0) 2023.05.23
[Java] 반복문 - while 문  (0) 2023.05.18