Final Keyword in Java

Advertisements

Java Final Keyword

Final keyword is used to make constant variable, to make class non-inheritable, to make method non-overridable and to make constant objects Final keyword can be used with:

  • Final variable
  • Final class
  • Final method
  • Final object

Final Variable

Final keyword can be used to make constant variables. We can't initialize final variable more than once or we can't re-initialize final variable.

Example of final keyword with variable


//FinalVariableDemo.java

    class FinalVariableDemo
    {
        public static void main(String[] arg)
        {
            final int a;

            a = 10;     //1st initialization

            System.out.print("\n\tA : " + a);

            a = 20;     //Compile-time error : 2nd initialization

            System.out.print("\n\tA : " + a);
        }
    }

    Output:
                Compile-time error - variable a might already have been assigned

Final Class

Final keyword can be used to make class non-inheritable. We can't inherit final class.

Example of final keyword with class


//FinalClassDemo.java

    final class BaseClass
    {
        void Display()
        {
            System.out.print("\n\tThis is Display() method of BaseClass.");
        }
    }

    class DerivedClass extends BaseClass    //Compile-time error - can't inherit final class
    {
        void Display()
        {
            System.out.print("\n\tThis is Display() method of DerivedClass.");
        }
    }

    class FinalClassDemo
    {
        public static void main(String[] arg)
        {
            DerivedClass D = new DerivedClass();

            D.Display();
        }
    }

    Output:
                Compile-time error - cannot inherit from final BaseClass

Final Method

Final keyword can be used to make method non overridable. We can't override final method.

Example of final keyword with method


//FinalMethodDemo.java

    class BaseClass
    {
        final void Display()
        {
            System.out.print("\n\tThis is Display() method of BaseClass.");
        }
    }

    class DerivedClass extends BaseClass
    {
        void Display()    //Compile-time error - can't override final method
        {
            System.out.print("\n\tThis is Display() method of DerivedClass.");
        }
    }

    class FinalMethodDemo
    {
        public static void main(String[] arg)
        {
            DerivedClass D = new DerivedClass();

            D.Display();
        }
    }

    Output:
                Compile-time error - overridden method is final

Final Object

Final keyword can be used to make constant objects. We can't initialize final object more than once or we can't re-initialize final object.

Example of final keyword with object


//FinalObjectDemo.java

    class FinalObjectDemo
    {
        int A,B;

        FinalObjectDemo(int a,int b)
        {
            A = a;
            B = b;
        }
        void Display()
        {
            System.out.print("\n\tA : " + A);
            System.out.print("\n\tB : " + B);
        }

        public static void main(String[] arg)
        {
            final FinalObjectDemo D = new FinalObjectDemo(3,5);

            D.Display();

            D = new FinalObjectDemo(7,2);       //Compile-time error - Can't re-initialize

            D.Display();
        }
    }

    Output:
                Compile-time error - cannot assign a value to final variable D

Advertisement