Java Programming Language – Interview Questions & Answers (Page -4)
Que. & Ans. Related to final, finally and finalize ()

What is final class in Java?

Final class is just like constant class (since constant class is not a programming term, just for understanding), which can not be sub classed i.e. a final class can not be inherited in the program. Mostly java standard library classes are final and their all methods are implicitly final.
Example of final classes in Java API:
java.lang.String, java.lang.Math, java.lang.System

What is final, finally & finalize () in Java?

final:
final is a keyword/modifier which is used with variable and class. Value of final variable can not be changed during program’s execution, while final class can not be inherited.

finally:
finally is used with exception, it is an exception handling code section which will definitely executed whether an exception occurred or not, it is generally used to free allocated memory or resources.

finalize ():
It is a method of Object class, which is called by the garbage collector just before garbage collecting when garbage collector collecting the unused references. It is used to give a final chance to collect unused references or freeing memory.

Can an interface be final?

No, an interface can not be final, because to make interface useful we must create sub classes. Only classes can be final.

Does System.Exit() in try block executes finally block?

System.Exit() statement terminates program, so finally block will not be executed after System.Exit() statement.
Consider the example
    public class JavaExp{
	    public static void main(String args[]){
			    try{ 
				    System.out.println("Try Block."); 
				    System.exit(0); 
			    } 
			    catch(Exception ex){ 
				    ex.printStackTrace(); 
			    }
			    finally { 
				    System.out.println("Finally Block."); 
			    } 
	    }
    }
    
    Try Block.
    

Can we use finally block without using catch block?

Yes, we can use finally block without using catch block. try block is necessary to use finally block.
Consider the example
    public class JavaExp{
	    public static void main(String args[]){
				    try{
					    System.out.println("Try Block."); 
				    }
			    finally { 
				    System.out.println("Finally Block."); 
			    } 
	    }
    }
    
    Try Block.
    Finally Block.
    

  • Page 4 of 4
  • 1
  • 2
  • 3
  • 4