The final keyword depends on where you use it:
public final class ...
, here you say that this class cannot be a super class (so no one can inherit this).
public final String someField
, here you say that the String someField
cannot be changed (after it has been initialized for the first time of course).
public final myMethod() { ... }
, here the method cannot be overriden in a subclass.
A finally block is used to get some code to run irrelevant whether the try catched the exception:
try {
Random rand = new Random();
if (rand.nextInt(2) == 0) { throw new Exception(); }
System.out.println("You'll get here 50% of the time (when no exception is thrown)");
catch (Exception ex) {
System.out.println("This should happen 50% of the time.");
} finally {
System.out.println("You will ALWAYS get here!");
}
The finally method is something like this: protected void finalize()
, this can be overriden by parent (@Override
), like this:
@Override
protected void finalize() {
System.out.println("Do something");
}
The finalize-method should be run when garbage collection decides to remove the object. However this only happens when there are no references to the object. However it has some serious downsides. I would only use it when I would have some native data stored somewhere that has to be freed. But you'll probably almost never do that.
Hope this helps.