* final 예약어
  - 마지막의, 변경될 수 없는.
  - 상수(constant)의 의미, 상수의 이름은 모두 대문자로 하는 것이 관례.
  - 적용분야 : 변수, 메소드, 클래스

 

1. 변수에 final을 적용할 때
  - final로 지정된 변수는 값을 변경할 수 없는 상수가 됨.
  - 프로그램이 끝날때까지 변수값을 변경하지 못하는 상수화


2. 메소드에 final을 적용할 때
  - 상속관계에 따른 오버라이딩으로의 확장을 할 수 없음.(재정의 할 수 없음)
  - 리폼안된다

 

3. 클래스에 final을 적용할 때
  - 더 이상 상속을 허용하지 않는 종단클래스
  - 다른 클래스의 조상이 될 수 없음.
  - 더이상 물려주지말고 너만 써라

 

파일명 : FinalEx.java

 

package oop3;

class Box
{
    final int rect=10; //변수를 상수화(constant)시킨것
    int poly=20;
    int circle=30;
    final static int VAR=123;
    final static String COMPANY="SAMSUNG";
    final static int PASS=1004;
   
    final void test(int n)
    {
        this.poly=n;
    }
   
    final static void test2()
    {
        //클래스이름으로 접근가능
        System.out.println("final static 메소드");
    }
}

final class SKY
{
   
}
//==================
public class FinalEx {

    public static void main(String[] args) {
        Box box=new Box();
        System.out.println(box.rect);
        System.out.println(box.poly);
        System.out.println(box.circle);
        box.poly=40;
        box.circle=50;
        //box.rect=60;
       
        System.out.println(Box.VAR);
        System.out.println(Box.COMPANY);
        System.out.println(Box.PASS);
/*
  class Math{
     final static double E=2.7182~~~~;
     final static double PI=3.14~~~;
  }
*/
        box.test(100);
        System.out.println(box.poly);
       
        //box.test2() 비추천
        Box.test2(); //강추 클래스명으로 직접접근
       
        System.out.println(Math.max(100,200));
       
    }
}

'..열심히 공부하세.. > JAVA 문법' 카테고리의 다른 글

[24] 상속  (0) 2012.05.01
[23] 접근제어자  (0) 2012.05.01
[21] static 예약어  (0) 2012.04.27
[20] this, this()  (0) 2012.04.26
[19] 변수의 유효범위 (Scope)  (0) 2012.04.26

+ Recent posts