Final Example]
- Final is used to apply restrictions on class, method and variable. Final class can’t be inherited, final method can’t be overridden and final variable value can’t be changed.
- Final is a keyword.
class FinalEg
{
public static void main(String[] args)
{
final int x = 600;
x = 400;// Compile Time Error
}
}
Finally Example
- Finally is used to place important code, it will be executed whether exception is handled or not.
- Finally is a block.
class FinallyEg
{
public static void main(String[] args)
{
try {
int x = 500;
} catch (Exception e) {
System.out.println(e);
} finally {
System.out.println("finally block is executed");
}
}
}
Finalize Example
- Finalize is used to perform clean up processing just before object is garbage collected.
- Finalize is a method.
class FinalizeEg
{
public void finalize()
{
System.out.println("finalize called");
}
public static void main(String[] args)
{
FinalizeExample f1 = new FinalizeExample();
FinalizeExample f2 = new FinalizeExample();
f1 = null;
f2 = null;
System.gc();
}
}