what are the differences between final,finally,finalize
methods?
Answer Posted / somi
Final:- It is used in the following three 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.
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.
| Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
Why singleton is not thread safe?
What is thread safe in java?
I want to control database connections in my program and want that only one thread should be able to make database connection at a time. How can I implement this logic?
How do you use wildcards?
What is the purpose of stub and skeleton?
What is the difference between Java and C++?
What is the purpose of void class?
What are the differences between c++ and java?
What is heterogeneous in java?
Which category the java thread do fall in?
How many static init can you have?
What is difference between final and finally in java?
what are the states associated in the thread? : Java thread
What is default size of arraylist in java?
How do I compare two strings in word in java?