JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Difference between final vs finally and finalize in Java?

Difference between final vs finally vs finalize in Java
The difference given in first paragraph about final, finally and finalize() method is sufficient from an interview point of view but it's better to know some more differences if you want to impress the interviewer or want to prepare better for follow-up questions. Let's see couple of more points to learn this concept better.


1) First and foremost, even though their name sounds similar, final, finally and finalize() are completely different from each other. There is no similarity in their function, whatever is, its only on their names.


2) The final keyword can be used with a class, method or variable in Java. A final class is not extensible in Java, a final method cannot be overridden, and a final variable cannot be changed. You can make a class Immutable in Java by using the final keyword. Similarly you can prevent overriding by using the final keyword with method and you can declare static final method to represent constant values.


3) You need either a catch or a finally block with try block in Java. Main advantage of the finally block is that its guaranteed to be executed even if your try block throws an exception. There is only one scenario when finally block doesn't get executed, when JVM crashes or you call System.exit() or Runtime.exit() from try block.

.