What are the uses of final, finally and finalize in java?
Answer Posted / javamasque
final: It is used to stop modifying further. It is used before class (top-level/inner), variable (class/instance) and method.
If class become final, it never be sub-classed.
If variable become final, it never be modified through the program or application.
If method become final, it never be override.
finally: It is used to release resources with try or try-catch block. It is always executed block irrespective of throw or return statement. It can be only stopped with infinite loop or System.exit(0). Previously file handling code used finally block to close InputStream or OutputStream object. Now finally block is not used to close these object. They are implicitly auto closed as they are implementing AutoClosable interface.
finalize(): As we override the protected finalize method to release resources. It is the final step where we can release resource (means either nullify the object or close the stream objects etc.). It is called before GC .But issue is the below points,
•There is no guarantee that it will be called or if called the resource will be released by GC immediately.
•We should not completely rely on this way of releasing memory.
•We can urge JVM to execute our finalize method with below statements but it has no guarantee that the objects will be freed immediately by GC.
System.runFinalization() OR Runtime.getRuntime().runFinalization()
| Is This Answer Correct ? | 5 Yes | 0 No |
Post New Answer View All Answers
What are the restriction imposed on a static method or a static block of code?
Is there any difference between nested classes and inner classes?
Is void a wrapper class?
What is the old name of java?
what is thread? : Java thread
How many return statement are allowed in a function?
What is the use of runnable interface?
How do you sort in ascending order in java?
Is 64bit faster than 32 bit?
What does a void function return?
hi am an engineering student and my next plan is for ms in either in us or australia i got my passport but i dont know anything bout visa can u give brief idea 1)How to get prepared for visa and 2)How to apply for top universities and 3)How to pay the fee and so on These all are basic questions plz give me a clear idea
What is an anonymous class in java?
Explain the Propertie sof class?
What is lambda expression in java?
What is a function in java?