Wednesday, May 14, 2008

final, finally, & finalize

final: in case of a variable, it represents that the variable is a constant and the variable can be assigned to only once. If the variable is a field of a class, it must be assigned to either in the constructor of the class or if it’s a static field then it should be assigned to a value at the time of the declaration only. Why? Because, all the static variables are created and initialized at the time of class loading, and you can’t change the value assigned to final variable. Makes sense…right?

‘The value of a final variable cannot be changed’ is completely true only for the primitive data types. In case of references, ‘final’ ensures the immutability of the reference only and not the immutability of the object pointed to by that reference. You can call the mutators on the object to change its value.

All the variables declared inside an interface are implicitly final (and static as well).

In case of a class, it means that the class can’t be inherited. In the standard Java library – the classes like String, Integer, etc. are declared final. If you think that sub-classing of your class may be potentially dangerous or if you think that there is no need for the clients to sub class your class then you should declare it final. It may help in the compiler optimization process. But not guaranteed in each and every case.

If you want to restrict only a method to be overridden and not the entire class, then you need to declare that method as ‘final’.

finally: this is a part of Java Exception Handling mechanism. Irrespective of whether an exception is thrown or not, the finally-block will always be executed. Even if you get some exception during the execution of the catch-block then also the runtime system ensures that the finally-block gets executed. We normally release all the system resources inside finally-block to avoid any accidental locking of the vital system resources. Read more - finally explained - common ques, uses, etc. >>

finalize: this method is called before the garbage collection of the object. We normally override it to dispose of system resources or to perform other cleanup activity, but we should not rely on this method. We never know when exactly the method will be called.

It’s not guaranteed which thread will invoke the finalize method for any given object. The general contract of finalize is that it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. You can override the finalize method and make it do anything you want including making the object available again to other threads, but that’s not the usual purpose of it.

If an uncaught exception is thrown by the finalize method, it’s ignored and finalization of that object terminates.

Liked the article? You may like to Subscribe to this blog for regular updates. You may also like to follow the blog to manage the bookmark easily and to tell the world that you enjoy GeekExplains. You can find the 'Followers' widget in the rightmost sidebar.


No comments: