How do I use the final keyword in Java?

The final modifier is used to mark a class final so that it cannot inherited, to prevent a method being overridden, and to prevent changing the value of a variable. Arguments of a method if declared as final is also can not be modified within the method.

package org.kodejava.example.fundamental;

public class FinalExample {
    // breed is declared final.
    // can't change the value assigned to breed
    public final String breed = "pig";
    private int count = 0;

    // sound() method is declared final, so it can't be overridden
    public final void sound() {
        System.out.println("oink oink");
    }

    // number parameter is declared final. can't change the value
    // assigned to number
    public int count(final int number) {
        // assign a value to number variable will cause a
        // compile-time error
        //
        // number = 1;

        count = +number;
        return count;
    }

    public static void main(String[] args) {
        FinalExample fe = new FinalExample();
        // assign a value to breed variable will cause a
        // compile-time error
        //
        // fe.breed = "dog";

        int number = fe.count(20);
    }
}

final class SubFinalExample extends FinalExample {

    // try to override sound() method of superclass will cause a
    // compile-time error
    //
    // public void sound() {
    //     System.out.println("oink");
    // }
}

// try to inherit a class that declared final will cause a
// compile-time error
//
class OtherFinalExample extends SubFinalExample {
}

Wayan Saryada

Programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply