Videos
final
final can be used to mark a variable "unchangeable"
private final String name = "foo"; //the reference name can never change
final can also make a method not "overrideable"
public final String toString() { return "NULL"; }
final can also make a class not "inheritable". i.e. the class can not be subclassed.
public final class finalClass {...}
public class classNotAllowed extends finalClass {...} // Not allowed
finally
finally is used in a try/catch statement to execute code "always"
lock.lock();
try {
//do stuff
} catch (SomeException se) {
//handle se
} finally {
lock.unlock(); //always executed, even if Exception or Error or se
}
Java 7 has a new try with resources statement that you can use to automatically close resources that explicitly or implicitly implement java.io.Closeable or java.lang.AutoCloseable
finalize
finalize is called when an object is garbage collected. You rarely need to override it. An example:
protected void finalize() {
//free resources (e.g. unallocate memory)
super.finalize();
}
- "Final" denotes that something cannot be changed. You usually want to use this on static variables that will hold the same value throughout the life of your program.
- "Finally" is used in conjunction with a try/catch block. Anything inside of the "finally" clause will be executed regardless of if the code in the 'try' block throws an exception or not.
- "Finalize" is called by the JVM before an object is about to be garbage collected.
final keyword
class
On a class it means you forbid to have a child class extending yours.
public final class finalClass
Attribute/Field
final MyObject value = new MyObject() means you won't be able to modify the instance of the object.
value = xxxx won't be allowed, but you still can modify the object itself value.field = "xxx";
Method
When you use final on a method, that means you'll forbid child classes that extends your class to override this method.
public final void finalMethod()
It can also be used on arguments, that means you don't allow other to modify the instance of the object you give.
public void myMethod(final MyObject myObject)
The end user won't be able to do myObject = ...
Finally block
finally block has nothing to do with final, it's used when catching Exception to ensure a part of code will be ran, wherever there is an exception or not.
try { ...}
catch {} // Optional
finally { // Code that will be ran, exception or not being thrown}

Finalize method
It's called on every object when it's destroyed (usually garbage collected).
Final:- It is used in the following cases:
- If the final keyword is attached to a variable then the variable becomes constant i.e. its value cannot be changed in the program.
- If a method is marked as final then the method cannot be overridden by any other method.
- If a class is marked as final then this class cannot be inherited by any other class.
- If a parameter is marked with final it becomes a read only parameter.
Finally:-
If an exception is thrown in try block then the control directly passes to the catch block without executing the lines of code written in the remainder section of the try block. In case of an exception we may need to clean up some objects that we created. If we do the clean-up in try block, they may not be executed in case of an exception. Thus finally block is used which contains the code for clean-up and is always executed after the try ...catch block.
Finalize:-
It is a method present in a class which is called before any of its object is reclaimed by the garbage collector. finalize() method is used for performing code clean-up before the object is reclaimed by the garbage collector.