Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Difference Between Final, Finally and Finalize in Java

Java Final

  • A Java keyword is "Final." It is a modifier for access. The classes, methods, and variables are all subject to the "final" keyword.
  • Final Variable

Table Of Content
  • Definition
  • Comparison Chart
  • Key Differences

  • The variable cannot be changed after a final keyword has been applied to it.
  • When a final variable is declared, it must be initialized.
  • Final variables are declared in UPPERCASE according to a widely used coding practice.
  • The final variable doesn't take up memory for each individual instance.

Final Methods

  • When a method in a class is marked as final, its subclass cannot override it.
  • The compiler has the ability to "inline" small methods that are marked as final, reducing function call overhead and enhancing performance.
  • Calls to overridden methods are dynamically resolved, but a method that has been marked as final cannot be overridden. So, at compile time, the function calling can be resolved.

Final Class

  • A class cannot be inherited by any subclass if it is marked as final.
  • When a class is declared final, all of its methods are also automatically declared final.
  • A class cannot be both "final" and "abstract" declared at the same time.


Example of Final keyword


final class vehicles{}

    class Nano extends vehicles{   // The Nano class cannot be subclass the final class vehicles
	void run(){   
	  System.out.println("Running");
	}  
	public static void main(String args[]){  
		Nano obj=new  Nano();  
	    obj.run();  
	}  
}





Output:

Compile Time Error   


Definition of Finally


  • The try/catch block is always connected to the "finally" block in Java code. After the try/catch block and before the code that follows it, the "finally" block is executed.
  • No matter if the exception is thrown or not, the "finally" block will be executed. The "finally" block is executed whenever an exception is thrown and no catch block can be found to handle it.
  • The "finally" block is executed just before the method returns to the caller when it exits the try/catch block as a result of an uncaught exception or an explicit return stateme nt.
  • The "finally" block is used to release memory or clean up the resources used in the "try" block. Although writing the "finally" block after the try/catch block is not required, but it is a good practice.


Example of Finally keyword


public class Main {	
    public static void main(String args[]){    	  		  
	    try{   // try block  
            int arr[]=new int[10];    
            arr[15]=15/0;    
            System.out.println(arr[15]);    
	    }  
	    catch(ArithmeticException e){   
	      System.out.println(e);  
	    }  
	    catch(ArrayIndexOutOfBoundsException e){   
	       System.out.println(e);  
	    } 
	    finally {  
	    	System.out.println("The finally block is always executed.");  
	    }  
	  System.out.println("This Code after finally block");  
   } 
}




Output:

 java.lang.ArithmeticException: / by zero
The finally block is always executed.
This Code after  finally block  


Definition of Finalize


  • A method in an object class is called finalize. A file handle or other non-java resource may be held by an object, in which case it needs to be released before being destroyed.
  • The garbage collector calls this method before completely destroying the object.
  • Before the object is destroyed, this method cleans up the area around it.


Example of Finalize keyword


public class Main {	
    public static void main(String[] args) {
        String str = "Hello India!";
        str = null;
        System.gc(); // using gc() to call the garbage collector 
        System.out.println("Garbage Collection End");   
    }   
    @Override
    protected void finalize(){
        System.out.println("Finalize method is called.");
    } 
}




Output:

Garbage Collection End 

Difference Between Final, Finally and Finalize in Java


Difference Between Final, Finally and Finalize in Java are mention below

Basis of Differences FINAL FINALLY FINALIZE
Basic In Java, "Final" is both a "Keyword" and a "access modifier." In Java, "finally" is a block. In Java, "finalize" is a method.
Applicable The keyword "final" can be used with classes, variables, and methods. The final block is one that is always included in the try-and-catch block. The method finalize() is applicable to objects.
Keyword Working style (1) The final variable, once declared, is a constant that cannot be changed.
(2) No subclass may override the final method.
(3) The final class is not inheritable.
(1) Whether or not there is an exception, the finally block executes the crucial code.
(2) The finally block deactivates all of the resources used in the try block.
Before an object is destroyed, the finalize method performs cleaning operations related to it.
Execution Only when we call the final method is it executed. When the try-catch block is executed, the finally block is executed. Its execution is not affected by the exception. The finalize method is invoked just prior to the destruction of the object.


Key Differences Between Final, Finally, and Finalize

  • Finally is an access modifier, finalize is a method, and finalize is a block.
  • The keyword final applies to classes, variables, and methods. Finally is a block associated with the try catch block that is used to handle exceptions, and finalize is a method that only works with objects.
  • Once declared as final, a variable becomes constant and cannot be reassigned, a method declared as final cannot be overridden, and a class declared as final can never be inherited. Finally, the finally block is used to clear out the resources used by the try and catch blocks. the finalize method plays a crucial role in managing an object's resources and ensuring their proper cleanup before the object is destroyed. This article explores the significance of the finalize method in Java Exception Handling and its vital role in resource management.

Exception Handling With Method Overridding Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
custom-exception

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.