Java

static 변수, static 메서드

MoonGa 2024. 5. 10. 15:42
  • 기본적으로 클래스 영역에 있는 변수, 메서드를 사용하기 위해선 무조건 객체화를 해야함

public class StaticTest {

   static int a = 10;                  // static 변수

   public static void call () {     // static 메서드

       System.out.println("static method call");

   }

 

   public static void main(String[] args) {

        // static은 객체화 없이도 사용가능

        System.out.println(a);

        call();

   }

}

  • static 변수, static 메서드는 객체화 없이 바로 호출해서 사용 가능함
  • 메서드 선언할때, static 키워드를 사용하면 객체 생성없이 접근이 가능하기에(모든 객체가 해당 메서드를 공유하는 느낌) 코테할때는 static메서드로 생성을 해주면 매번 객체를 만들지 않아도 됨.