final — keyword preventing change. finally — block that always runs. finalize() — deprecated GC hook.
Analogy: final = locked door (can't open). finally = cleanup crew (always comes). finalize() = obsolete inspector.
final (3 uses):
1final int MAX = 100;2// MAX = 200; // COMPILE ERROR34final List<String> list = new ArrayList<>();5list.add("item"); // OK6// list = new ArrayList<>(); // COMPILE ERROR
1class Parent {2 final void lock() { System.out.println("Locked"); }3}4class Child extends Parent {5 // void lock() { } // COMPILE ERROR6}
1final class Immutable {2 private final int val;3 Immutable(int v) { val = v; }4}5// class Sub extends Immutable { } // COMPILE ERROR
finally:
1FileInputStream fis = null;2try {3 fis = new FileInputStream("data.txt");4} catch (IOException e) {5 System.err.println("Error");6} finally {7 if (fis != null) try { fis.close(); } catch (IOException e) { }8}910// Modern: try-with-resources11try (FileInputStream fis2 = new FileInputStream("data.txt")) {12 // use...13} catch (IOException e) { }14// fis2 auto-closed
finalize() (DEPRECATED):
1class Resource implements AutoCloseable {2 @Override3 public void close() {4 System.out.println("Closed");5 }6}7try (Resource r = new Resource()) {8 // use...9} // close() called
Summary: final = constant. finally = always runs. finalize() = deprecated.