What is the difference between final, finally, and finalize in Java?
-
-
Answer:
Have a look to this answer over Stackoverflow http://stackoverflow.com/questions/7814688/in-java-what-purpose-do-the-keywords-final-finally-and-finalize-fulfil
Arpit Srivastava at Quora Visit the source
Other answers
Final When applied to a variable (primitive): The value of the variable cannot change. When applied to a variable (reference): The reference variable cannot point to any other object on the heap. When applied to a method: The method cannot be overridden. When applied to a class: The class cannot be subclassed. Finally There is an optional finally block after the try block or after the catch block. Statements in the finally block will always be executed (except if JVM exits from the try block). The finally block is used to write the clean up code. Finalize This is the method that the JVM runs before running the garbage collector.
Vimox S Shah
final - final keyword can be used with a class, variable or a method. A variable declared as final acts as constant, which means one a variable is declared and assigned , the value cannot be changed. An object can also be final, which means that the once the object is created it cannot be assigned a different object, although the properties or fields of the object can be changed. A final class is immutable, which means that no other class can extend from it. E.g String, Integer. A final method in a class cannot be overridden in the child class. finally - finally keyword is used with try-catch block for handling exception. The finally block is optional in try-catch block. The finally code block is always executed after try or catch block is completed. The general use case for finally block is to close the resources or clean up objects used in try block. For e.g. Closing a FileStream, I/O stream objects, Database connections, HTTP connections are generally closed in a finally block.finalize() - This is the method of Object class. It is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. Should not be used to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these non-memory resources through the finalize() method.Source taken from http://java-questions.com/difference-between-final-finally-and-finalize.html
Harshit Rastogi
FinalFinal offers restrictions on classes,methods and variables.One they are final,they cannot be changed(immutable).public class Test { private static final String PREFIX = "test." ; private final MyClass obj = new Myclass(); publc Test() { obj = new MyClass() ;// throws error obj is final so we cant instantiate it again}}public class Test { private static final String PREFIX = "test." private final MyClass obj; publc Test() { obj = new MyClass() ;// this works because obj is instantiated in constructor }}FinallyFinally is a block used with try ,catch http://block.It will always get executed after try or catch block.try { //code...} catch (Exception e) { //code...} finally { System.out.println("This line is always printed");}Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.When closing a file or otherwise , place the code in a finally block.Finalisewhen the memory is overloaded with objects the system.gc(; will call finalize methodWhen the memory is getting overloaded then the finalize method will be called.public class TestGarbageCollection { public static void main(String[] args) { while (true) { TestClass s = new TestClass(); s.display(); System.gc(); } }}
Satyajeet Sen
final: final is a keyword. The variable decleared as final should be initialized only once and cannot be changed. Java classes declared as final cannot be extended. Methods declared as final cannot be overridden. finally: finally is a block. The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling - it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated. finalize: finalize is a method. Before an object is garbage collected, the runtime system calls its finalize() method. You can write system resources release code in finalize() method before getting garbage collected.
Akash Dighe
Related Q & A:
- What Is The Difference Between Magicjack And Magicjack Plus?Best solution by Yahoo! Answers
- what is the difference between sum of first n primes and prime(prime(n?Best solution by Mathematics
- What's the difference between Fullmetal Alchemist and Fullmetal Alchemist: Brotherhood?Best solution by Yahoo! Answers
- What's the difference between Current (I) and Potential Difference (V?Best solution by diffen.com
- What does it mean to be unresponsive? What's the difference between unresponsive and unconscious?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.