final

The final keyword can be applied to classes, methods, and variables. The meaning is literal: it makes something final. Here is how it behaves.


final and Classes

When you declare a class as final, no other class can extend it. This often supports security and immutability. For example, java.lang.String is final.

final class MadPlay {
    // ...
}

class MadClass extends MadPlay { // compilation error
    // ...
}

Because you cannot extend it, you also cannot override its methods. However, a final class can still extend another class.

class MadMan {
    // ...
}

final class MadPlay extends MadMan {
    // ...
}


final and Methods

A method declared final cannot be overridden.

class MadClass {
    public final void sayHi() {
        // ...
    }
}

public class MadPlay extends MadClass {
    // compilation error
    // cannot override... overridden method is final    
    public void sayHi() {
        // ...
    }
}


final and Variables

A variable declared final acts as a constant. You can assign it only once. So you typically initialize it at declaration time.

public class MadPlay {
    final int MAX_ARRAY_SIZE = 5;

    public void someMethod() {
        int[] array = new int[MAX_ARRAY_SIZE];

        // compilation error
        MAX_ARRAY_SIZE = 10;
    }
}

But final fields do not always need to be initialized inline. This code is valid:

public class Person {
    private final String name;

    // if you provide a default constructor, initialize here
    public Person() {
        this.name = "kimtaeng";
    }

    public Person(String name) {
        this.name = name;
    }
}

Here, the final field is initialized in the constructor, which is also valid.


Why Most Constants Are public static

Most final constants are declared like this so they can be shared throughout the project:

class GradeHelper {
    public static final String MAX_GRADE = "A+";
}

Why public static? public makes it accessible across classes and packages.

static allocates it once at class load time. It does not allocate per instance, which makes it ideal for fixed values. For example, a maximum grade of A+.