The "final" keyword is a modifier used in programming to indicate
that something cannot be changed or modified. It can be applied to
classes, variables and methods.When final keyword is added to a
class, it means that the class cannot be extended or subclassed. In
other words, we can say cannot create a new class that inherits from
a final class.The value of variable can't be changed if a variable
is declared as final.
For example If you declare a variable as final and assign it a value
of 10, its value cannot be changed later in the programme.The final
keyword is used when you want to make sure that specific varibale
remain constant throughout the program.
The final keyword is useful when you want a variable, such as PI, to
always store the same value (3.14).If a method has the final keyword
then it cannot be overridden by a subclass. A method defined in a
superclass can be implemented by a subclass, but if the method in
the superclass is marked as final then subclass cannot change or
override it.It helps in maintaining the integrity and stability of
your code by preventing accidental modifications.
It is necessary to initialize a final variable. otherwise, the compiler will raise a compile-time error. An initializer or assignment statement may only be used once to initialize a final variable.
When a variable is declared with the final keyword, its value is unchangeable; it is
effectively a constant. This also implies that a final variable must be initialized.
If a final variable is a reference type (such as an object or an array), it cannot
be rebound to point to a different object. However, you can still modify the
internal state of the object that the final variable references. This means you can
add or remove elements from an array or collection that is declared as final.
It is best to represent final variables in all uppercase, with underscores used to separate words.
final int num = 5; // Declaring a final variable of type int final String message = "Hello"; // Declaring a final variable of type String final int[] numbers = {1, 2, 3}; // Declaring a final array final List<String> names = new ArrayList<>(); // Declaring a final collection names.add("Ram"); // Modifying the internal state of the final collection numbers[0] = 10; // Modifying the first element of the final array System.out.println(num); // Output: 5 System.out.println(message); // Output: Hello System.out.println(Arrays.toString(numbers)); // Output: [10, 2, 3] System.out.println(names);
Output:
5
Hello
[10, 2, 3]
[Ram]
class Nano{ final int speedlimit=90;// it is final variable void run(){ speedlimit=400; // can't reassign value } public static void main(String args[]){ Nano obj=new Nano(); obj.run(); } }
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The final field Nano.speedlimit cannot be assigned
A method is referred to as a final method if the keyword final is used in its declaration. An ultimate method cannot be changed. This is done by the Object class, which has a few final methods. Methods that must have the same implementation across all derived classes must be declared with the final keyword.
class vehicles{ final void run(){ System.out.println("Running"); } } class Nano extends vehicles{ void run(){ // Cannot override the final method from vehicles System.out.println("Running"); } public static void main(String args[]){ Nano obj=new Nano(); obj.run(); } }
Output:
Compile Time Error
A class is referred to as a final class if the keyword final is used in its declaration. There is no way to extend a final class (inherited).
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
class Vehicles{ String vehiclesName(final String name){ return "nano"; } public static void main(String args[]){ Vehicles vehicles=new Vehicles(); System.out.println(vehicles.vehiclesName("Alto")); //value not updated } }
Output:
nano
class Vehicles{ static final int speed; // it is static blank final variable static{ speed=50; } public static void main(String args[]){ System.out.println(Vehicles.speed); } }
Output:
50
class nano{ final void run() { System.out.println("Nano"); } } class Vehicles extends nano{ public static void main(String args[]){ Vehicles vehicles=new Vehicles(); vehicles.run(); } }
Output:
50
Post your comment