2018년 4월 17일 화요일

(4. 17.) static멤버, static block, singleton, final

1. static 멤버
   ○ 클래스 멤버: 객체(인스턴스) 소속이 아닌 클래스 소속이라는 의미
      - static filed, static method, static block
   ○ why use?
      - 객체마다 가지고 있을 필요성이 없다
      - 공용이다
      - 예를 들어 원주율을 구할 때 파이는 공용 데이터 성격이므로 static double pi;
      - 인스턴스 필드를 사용하지 않는다면 static method로 선언
   ○ 사용법
      - Television.info: 클래스명.필드명 / 클래스명.메소드명
      - dot(.)로 사용
      - 객체 생성자를 이용하여 객체로도 사용 가능 --> 그럴거면 왜 만드냐??
   ○ why static block use?
      - static field 의 계산이 필요한 경우



   ○ 주의사항
      - this 사용불가
      - static block, static method 내에서 instance filed 사용 불가(why? 메모리를 생각해봐)
      - static block, static method에서 instance member 사용하고 싶다면 객체 생성 후 사용가능

3. Singleton
   ○ 전체 프로그램에서 단 하나의 객체 생성만 보장해야 하는 경우 사용
       for the lifetime of the routine or the application.


4. final
   ○ 최종값, 변하지 않는 상수
   ○ 초기값할당 
      1. 선언 시
      2. 생성자
         ==> 초기값 할당되지 않은 final 필드는 컴파일 에러
   ○ 상수가 아닌 이유는
       1. 객체마다 생성
       2. 생성자를 통해 여러 값을 가질 수 있다.(불변한다는 것이지 값이 전부 똑같다는 것은 아니잖아)
       3. 상수가 되려면 static final 이어야겠찌? --> 이 경우는 대문자로


댓글 없음:

댓글 쓰기

[Android Studio] Installation and Make a Layer for Mac OS(High Sierra)

1. download android studio https://developer.android.com/studio/#downloads 2. Make a Shared Resource(String in this case) 3...