final:
- final is a keyword
- final is used to apply restrictions on class, method and variable
- final class cannot be inherited
- final method cannot be overriden
- final variable value cannot be changed
- class FinalExample {
- public static void main(String[] args) {
- final int x = 100;
- x = 200;// Compile Time Error
- }
- }
finally:
- finally is a block
- finally block always execute, whether exception is handled or not
- in one scenario finally block doesn't execute is System.exit(0);
- class FinallyExample {
- public static void main(String[] args) {
- try {
- int x = 300;
- } catch (Exception e) {
- System.out.println(e);
- } finally {
- System.out.println("finally block is executed");
- }
- }
- }
- finalize() is a method
- finalize is used to perform clean up processing just before object is garbage collected
- class FinalizeExample {
- 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();
- }
- }
No comments:
Post a Comment