Java / Exception
Difference between final, finally and finalize in Java.
final keyword is used to make a variable or a method or a class as "unchangeable".
Final variable: A variable which is declared as final, its value cannot be changed once it is initialized.
class FinalKeywordExample { public static void main(String[] args) { final int x = 10; x = 200;// Compile Time Error } }
A final method declared as final can not be overridden or modified in the subclass.
class Parent { final void parentMethod() { } } class Child extends Parent { //Compilation error public void parentMethod() { } }
A final class cannot be extended.
final class Parent { void parentMethod() { } } //Compilation error class Child extends Parent { }
finally block is used for exception handling along with try and catch blocks. finally block is always executed whether an exception is raised or not and raised exception is handled or not. Most of the time, this block is used to close the resources like database connection, I/O resources.
try { // do stuff } catch { // handle errors } finally { // clean up connections etc. }
finalize method is a protected method of java.lang.Object class. It is inherited by every class you create in Java. This method is called by garbage collector thread before an object is removed from the memory. finalize() method is used to perform some cleanup operations on an object before it is removed from the memory.
public class FinalizeTest { public static void main(String[] args) throws Throwable { FinalizeTest fTest = new FinalizeTest(); FinalizeTest fTest2 = new FinalizeTest(); fTest = null; fTest2 = null; System.gc(); System.out.println("Main Thread complete."); } @Override protected void finalize() throws Throwable { super.finalize(); System.out.println("finalize method overriden"); } }
Dogecoin
! Earn free bitcoins up to $250 now by signing up.
Earn bitcoins upto $250 (free), invest in other Cryptocurrencies when you signup with blockfi.
Use the referral link: Signup now and earn!
Using BlockFi, don't just buy crypto - start earning on it. Open an interest account with up to 8.6% APY, trade currencies, or borrow money without selling your assets.
Join CoinBase
! We'll both receive $10 in free Bitcoin when they buy or sell their first $100 on Coinbase! Available in India also.
Use the referral Join coinbase!
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull
! Receive free stock by signing up using the link: Webull signup.
More Related questions...