8. What is the 'static' keyword means ?

toutbon·2024년 2월 25일
0

질문의 목적

  • java 에서 static의 개념을 잘 이해하고 있는 지

Static 의 개념

  • Java는 객체지향 언어이기 때문에 대부분의 경우 필요할때 마다 객체를 메모리에 올려서 사용해야한다
  • static은 프로그램에 의한 객체 생성 이전에 JVM에 의해서 클래스가 로딩되는 순간에 메모리에 자동으로 올라오게 된다 --> initialized when the class is first loaded
  • 생성되는 객체 차원 이전에 클래스 이름으로 접근이 가능한 메모리
  • 클래스로부터 생성된 객체와 관련이 없다
  • static의 영역에서 non-staitc의 영역(객체의 영역)으로는 접근 불가능 ( you can't access non-static variables or method from static)
  • static 키워드는 자바에서 변수와 메서드 static initialize/ static block/ nested class 에서 선언가능 --> 주로 변수와 메서드에서 사용함

nested class 란?

https://limdevbasic.tistory.com/28

static variables

  • 객체 생성전 클래스의 이름으로 접근 가능
  • 각각의 객체별로 선언되는 것이 아니고, 클래스 단에서 가지고 있기 때문에 어느곳에서 접근해도 같은 값이다 -> The same across all instances of the class
  • 전역변수가 필요할때 사용함

static method

  • 클래스 레벨에서 호출이 가능한 메서드
  • 객체생성을 한 후 레퍼런스를 이용해서 호출하지 않는다
  • 프로그램이 로딩되면서 사용이 되어야 하는 경우 (Starting point)
    ex) public static void main(String[] args)
  • 빈번하게 사용하는 경우 (popular method)
    ex ) Math.round()

Keyword

  • initialized when the class is first loaded
  • class level access, accessible before creating object
  • static variables, static method
profile
뚜봉

0개의 댓글