What are the uses of final, finally and finalize in java?

Answers were Sorted based on User's Feedback



What are the uses of final, finally and finalize in java?..

Answer / 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

What are the uses of final, finally and finalize in java?..

Answer / ch.soundarya

final is a keyword which is used on variable,method as well as on class.the variable with final keyword cannot be re used.and the method with final cannot be overrriden.the class with final keyword cannont be inherited.

FINALIZE:is amethod used for garbage collection .we can force finalize method explicitly for garbage collection.

FINALLY: it is method is used for close the allocated resources in a running program of java class.

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More Core Java Interview Questions

What restrictions are placed on method overloading?

0 Answers  


Can we change the scope of the overridden method in the subclass?

0 Answers  


How arrays are stored in memory in java?

0 Answers  


What is meant by class loader and how many types are there?

2 Answers   Apple,


I don’t want my class to be inherited by any other class. What should I do?

0 Answers  






if the memory capacity is 700 presently occupied by process is 690. then another process request space(40) how this situation handled in java.

4 Answers   Wipro,


What is parsing in grammar?

0 Answers  


Can you sort a string in java?

0 Answers  


Why collection doesn’t extend cloneable and serializable interfaces?

0 Answers  


What is scanner in java?

0 Answers  


4.1 Supply contracts (in the form of comments specifying pre- and post conditions) for the enqueue() method of the LinkedQueue class given in the Appendix. (2) 4.2 Let Thing be a class which is capable of cloning objects, and consider the code fragment: Thing thing1 = new Thing(); //(1) Thing thing2 = thing1; //(2) Thing thing3 = (Thing) thing1.clone(); //(3) Explain how the objects thing2 and thing3 differ from each other after execution of the statements. (

0 Answers  


int a=10,b=20,c=30 a= b+c;b=a+c;c=a+b; System.out.println("The value is"+a+b+c;

17 Answers   Honeywell,


Categories