Final Keyword in Java

Final Keyword

In this article we will be discussing about Final Keyword in Java.

The Final keyword is used to initialize Constant variables and many more. We can use final  with variables, methods and classes.

Final Keyword in Java

Final :

The Final keyword is kind of a constant keyword in c++, just like constant, in Java Final keyword is used to initialize constant declaration after which their values can’t be changed throughout the program.

We can initialize following declarations with the help of final keyword:

  • variable
  • method
  • class

Final Variable:

You can declare any final variable using “final” Keyword. you’ll not be able to change this value afterwards throughout the program i.e final variable once initialized with a value can’t be change furthermore.

final int str = "PrepInsta";
Run
import java.util.*;

public class Main{
    // class definition 
    class Prepster{

        // defining final variable
        final int test = 10;
        void change(){
            test = 20;
        }

    public static void main(String[] args){
        // Initializing the class object 
        prepster obj = new prepster();
        obj.change();
    }
}
Output:

Compile Time Error

In Java, a final variable is a variable that once assigned a value, its value cannot be changed. Attempting to change the value of a final variable will result in a compile-time error.
This code will result in a compile-time error with the message: “error: cannot assign a value to final variable test”.

Final Function:

If you are using the final keyword with the method in java, you will not be able to override in the program.

Run
import java.util.*;

// Definition of the class
class Lakshit{
    // Final method in java
	final void test(){
       System.out.print("final Function");
    }
}
public class Main{	
    public static void main(String[] args){
		// Initializing the class object
        Lakshit obj = new Lakshit();
		
		// Function calling with the help of class object
        obj.test();
    }
}
Output:

final Function

Java Final Class:

if we use Final keyword with class, we’ll not be able to extend it.

Run
import java.util.*;

// final Class definition 
final class test {
	// function definition
    void run(){
        System.out.println("test class called");
    }
}

public class Main{      
    public static void main(String[] args){ 
		//Initializing the object of the final class
        test obj = new test();  
		
		// Calling the run function of final class
        obj.run();  
    }  
}
Output:

test class called

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription