[ACCEPTED]-Java `final` method: what does it promise?-final

Accepted answer
Score: 164

As mentioned, final is used with a Java method 29 to mark that the method can't be overridden 28 (for object scope) or hidden (for static). This 27 allows the original developer to create 26 functionality that cannot be changed by 25 subclasses, and that is all the guarantee 24 it provides.

This means that if the method 23 relies on other customizable components 22 like non-public fields/methods the functionality 21 of the final method may still be customizable. This 20 is good though as (with polymorphism) it 19 allows for partial customization.

There are 18 a number of reasons to prevent something 17 from being customizable, including:

  • Performance -- Some 16 compilers can analyse and optimise the operation, especially 15 the one without side-effects.

  • Obtain encapsulated data -- look at 14 immutable Objects where their attributes 13 are set at the construction time and should 12 never be changed. Or a calculated value 11 derived from those attributes. A good example 10 is the Java String class.

  • Reliability and Contract -- Objects are composed 9 of primitives (int, char, double, etc.) and/or other 8 Objects. Not all operations applicable to 7 those components should be applicable or 6 even logical when they are used in the bigger 5 Object. Methods with the final modifier can be 4 used to ensure that. The Counter class is 3 a good example.


public class Counter {
    private int counter = 0;

    public final int count() {
        return counter++;
    }

    public final int reset() {
        return (counter = 0);
    }
}

If the public final int count() method is not final, we 2 can do something like this:

Counter c = new Counter() {   
    public int count() {
        super.count();   
        return super.count();   
    } 
}

c.count(); // now count 2

Or something 1 like this:

Counter c = new Counter() {
    public int count() {
        int lastCount = 0;
        for (int i = super.count(); --i >= 0; ) {
            lastCount = super.count();
        }

        return lastCount;
    }
}

c.count(); // Now double count
Score: 29

What kind of "contract" does a final method 7 promise?

Look at it the other way, any non 6 final method makes the implicit guarantee that you can override 5 it with your own implementation and the 4 class will still work as expected. When 3 you can't guarantee that your class supports 2 overwriting a method you should make it 1 final.

Score: 8

First of all, you can mark non-abstract 14 classes final as well as fields and methods. This 13 way whole class can't be subclassed. So, behavior 12 of class will be fixed.

I agree that marking 11 methods final don't guarantee that their behavior 10 will be the same in subclasses if these 9 methods are calling non-final methods. If 8 behavior is indeed need to be fixed, this 7 has to be achieved by convention and careful 6 design. And don't forget to notion this 5 in javadoc!(java documentation)

Last but 4 not the least, final keyword has very important 3 role in Java Memory Model (JMM). It's guaranteed 2 by JMM that to achieve visibility of final fields 1 you don't need proper synchronization. E.g.:

class A implements Runnable {
  final String caption = "Some caption";                           

  void run() {
    // no need to synchronize here to see proper value of final field..
    System.out.println(caption);
  }
}  
Score: 0

No, it's not outside the influence of the 5 class author. You can't override it in your 4 derived class, therefore it will do what 3 the base class author intended.

http://download.oracle.com/javase/tutorial/java/IandI/final.html

Worth noting 2 is the part where it suggests that methods 1 called from constructors should be final.

Score: 0

I'm not sure you can make any assertions 9 about the use of "final" and how that impacts 8 the overall design contract of the software. You 7 are guaranteed that no developer can override 6 this method and void its contract that way. But 5 on the other hand, the final method may 4 rely on class or instance variables whose 3 values are set by subclasses, and can call 2 other class methods that are overridden. So 1 final is at most a very weak guarantee.

More Related questions