final, finally , and finalize in java

In java program, there are many key words that programmer have to remember. Among that key words are confuse the programmer. In this post will explain 3 (final, finally, finalize) key word that some starter always get confuse.

  1. Final
    • Keyword in java
    • In front of data member (will become constant )
    • In from of method member (final method): this method will cannot overridden. Example: wait(), notify(), notifyAll().
    • In front of class : final class, this will cannot extended by subclass (stopped inherited )
//final class
public final class MyClass {
	private int member1;
//	fianl data member 
	private final int member=5;
	
//	final method
	public final void medod1(){
		System.out.println("method 1");
	}
	
	public void medod2(){
		System.out.println("method 2");
	}
}

 

  1. Finally
    • Is it a block
    • It is used for exception handling along with try and catch.
    • It will be executed whether exception is handled or not, except when we use exit(0).
    • It is used to cleanup code or resource. Example : DB conncetion.

** example try catch without exception

public class TestFinally {
	public static void main(String[] args) {
//		 try catch without exception 
		String value1="10";
		try{
			int x =Integer.parseInt(value1);
			System.out.println(" the value : "+x);
		}catch (NumberFormatException e) {
			System.out.println("exception block ");
//			any handler here
		}finally {
			System.out.println("here is finally block ");
		}
	}

}

output :

the value : 10
here is finally block 

** example try catch with exception

public class TestFinallyExc {
	public static void main(String[] args) {
//		 try catch with exception 
		String value1="10ee";
		try{
			int x =Integer.parseInt(value1);
			System.out.println(" the value : "+x);
		}catch (NumberFormatException e) {
			System.out.println("exception block ");
//			any handler here
		}finally {
			System.out.println("here is finally block ");
		}
	}

}

** example try without catch

public class TestFinallyNoCatch {
	public static void main(String[] args) { 
		String value1="10";
		try{
			int x =Integer.parseInt(value1);
			System.out.println(" the value : "+x);
		} finally {
			System.out.println("here is finally block ");
		}
	}

}

** example with exit(0)

public class TestFinally2 {
	public static void main(String[] args) {
//		 try catch without exception 
		String value1="10";
		try{
			int x =Integer.parseInt(value1);
			System.out.println(" the value : "+x);
			System.exit(0);
		}catch (NumberFormatException e) {
			System.out.println("exception block ");
//			any handler here
		}finally {
			System.out.println("here is finally block ");
		}
	}

}

Output :

the value : 10
  1. Finalize
    • It is a method called by Garbage collection thread before collecting eligible objects to perform clean up processing.
    • This is the last chance for object to perform any clean-up.
    • It’s not guaranteed that whether finalize() will be called, we should never override finalize method.

Add a Comment

Your email address will not be published. Required fields are marked *