final可以用来修饰变量、类、方法,final修饰的变量不可修改,final修饰的类不可扩展,final修饰的方法不可重写。
finally是保证重点代码一定要被执行到的机制。
finalize是Object的一个方法,它的设计目的是保证对象在被垃圾收集前完成特定资源的回收,已经不被推荐使用。推荐使用Cleaner机制:
public class CleaningExample implements AutoCloseable { // A cleaner, preferably one shared within a library private static final Cleaner cleaner = <cleaner>; static class State implements Runnable { State(...) { // initialize State needed for cleaning action } public void run() { // cleanup action accessing State, executed at most once } } private final State; private final Cleaner.Cleanable cleanable public CleaningExample() { this.state = new State(...); this.cleanable = cleaner.register(this, state); } public void close() { cleanable.clean(); } }
final不是immutable,java语言没有原生的不可变支持,如果要实现immutable的类,需要做到: