final — keyword preventing modification. finally — block that always runs. finalize() — deprecated GC callback.
Analogy: final = sealed envelope (can't change). finally = janitor who always cleans up. finalize() = obsolete building inspector.
final keyword (3 uses):
1final int MAX = 100;2// MAX = 200; // COMPILE ERROR34final List<String> list = new ArrayList<>();5list.add("item"); // OK — modify object6// list = new ArrayList<>(); // COMPILE ERROR — reassign
1class Parent {2 final void important() { System.out.println("Locked method"); }3}4class Child extends Parent {5 // void important() { } // COMPILE ERROR6}
1final class Immutable {2 private final int value;3 Immutable(int v) { this.value = v; }4}5// class Sub extends Immutable { } // COMPILE ERROR
finally block:
1FileInputStream fis = null;2try {3 fis = new FileInputStream("data.txt");4} catch (FileNotFoundException e) {5 System.err.println("Not found");6} finally {7 if (fis != null) try { fis.close(); } catch (IOException e) { }8 System.out.println("Cleanup done");9}1011// Better: try-with-resources12try (FileInputStream fis2 = new FileInputStream("data.txt")) {13 // use file...14} catch (IOException e) {15 System.err.println("Error");16}17// fis2 closed automatically
finalize() (DEPRECATED):
1// BAD: old approach2class OldResource {3 @Override4 protected void finalize() throws Throwable {5 try { /* cleanup */ } finally { super.finalize(); }6 }7}89// GOOD: modern approach10class ModernResource implements AutoCloseable {11 @Override12 public void close() {13 System.out.println("Closed deterministically");14 }15}16try (ModernResource r = new ModernResource()) {17 // use resource...18} // close() called immediately
Summary: final = constant. finally = always runs. finalize() = deprecated, use AutoCloseable.