0% found this document useful (0 votes)
4 views

Interface in Java with Examples

The document explains the final keyword in Java, detailing its use with variables, methods, and classes to restrict modification. It covers final variables, including how to initialize blank final variables, and illustrates the concept with examples. Additionally, it discusses final methods that cannot be overridden and final classes that cannot be inherited, emphasizing the importance of these features in maintaining code integrity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Interface in Java with Examples

The document explains the final keyword in Java, detailing its use with variables, methods, and classes to restrict modification. It covers final variables, including how to initialize blank final variables, and illustrates the concept with examples. Additionally, it discusses final methods that cannot be overridden and final classes that cannot be inherited, emphasizing the importance of these features in maintaining code integrity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Final Keyword in Java with Examples

In this article, I am going to discuss the Final Keyword in Java with


Examples. At the end of this article, you will understand the following
points in detail.
1. What is Java Final Keyword?
2. Final Variables in Java
3. How to initialize a blank final variable?
4. When to use Final Variable in Java?
5. What is the Reference Final variable?
6. Final Methods in Java
7. Final Class in Java

What is the Final Keyword in Java?


In the Java programming language, the final keyword is used in several
contexts to define an entity that can only be assigned once. The final
keyword is used in different contexts. First of all, the final is a non-access
modifier applicable only to a variable, a method, or a class. Following are
the different context of using the final keyword:

The final keyword in java is used to restrict the user.

In Java, the final keyword can be used while declaring an entity.


Using the final keyword means that the value can’t be modified in the
future.

Final can be:


• Variables
• Methods
• Classes

Final Variables in Java

• When a variable is declared with the final keyword, its value


can’t be modified, essentially, a constant.
• This also means that you must initialize a final variable.
• The final variable cannot be reinitialized with another value.
• However, the data within the object can be changed. So, the
state of the object can be changed, but not the reference.
• With variables, the final modifier often is used with static to
make the constant a class variable.

Note :
• The variable does not necessarily have to be initialized at the time
of declaration. If it’s declared but not yet initialized, it’s called
a blank final variable. This approach is the most common.
• It is recommended to use uppercase to declare final variables in
Java.

How to initialize a blank final variable?


Below are the two ways to initialize a blank final variable:
1. A blank final variable can be initialized inside the instance-initializer
block or inside the constructor. If you have more than one
constructor in your class then it must be initialized in all of them,
otherwise, a compile-time error will be thrown.
2. A blank final static variable can be initialized inside a static block.
When to use Final Variable?
Final Variables must be used only for the values that we want to remain
constant throughout the execution of the program because a final
variable is that we can re-assign value to a normal variable but we
cannot change the value of a final variable once assigned.

Sample Program to demonstrate the final variable in Java:


public class FinalVariableDemo
{
final int speedlimit = 90; //final variable

void run ()
{
speedlimit = 400;
}

public static void main (String args[])


{
FinalVariableDemo obj = new FinalVariableDemo ();
obj.run ();
}
}
Output: Compile-Time Error

There is a final variable speed limit, we are going to change the value of
this variable, but it can’t be changed because the final variable once
assigned a value can never be changed.

What is Reference Final Variable in Java?


The final reference allows you to modify the state of the object but you
cannot modify the reference variable to point to a different object in the
memory.
For Example, final Person p1;

In the case of a reference final variable, the internal state of the object
pointed by that reference variable can be changed.
Note that this is not re-assigning.

This property of the final is called non-transitivity.

Example to demonstrate Reference Final Variable in Java:


public class Person
{
String name;
int age;

public static void main (String[]args)


{
final Person p1 = new Person (); //Reference Final Variable
p1.age = 12;
p1.name = "John";

//This is legal. You can modify the state of final reference.


p1.age = 30;
p1.name = "Peter";
}
}
Q) What is blank or uninitialized final variable?
A final variable that is not initialized at the time of declaration is known
as blank final variable.

If you want to create a variable that is initialized at the time of creating


object and once initialized may not be changed, it is useful. For example
PAN CARD number of an employee.
It can be initialized only in constructor.

Example of blank final variable


class Student
{
int id;
String name;
final String PAN_CARD_NUMBER;
...
}

Que) Can we initialize blank final variable?

Yes, but only in constructor. For example:


class Bike10{
final int speedlimit;//blank final variable

Bike10(){
speedlimit=70;
System.out.println(speedlimit);
}

public static void main(String args[]){


new Bike10();
}
}

Final Methods in Java


A final method cannot be overridden or hidden by subclasses which
means even though a subclass can call the final method of parent class
without any issues but it cannot override it. This is used to prevent
unexpected behavior from a subclass altering a method that may be
crucial to the function or consistency of the class. We must declare
methods with the final keyword for which we are required to follow the
same implementation throughout all the derived classes.
The main intention of making a method final would be that the content
of the method should not be changed by any outsider.

Example demonstrating Final Methods in Java:


class Bike
{
final void run()
{
System.out.println ("running");
}
}
class Honda extends Bike
{
void run()
{
// COMPILE-ERROR! Can't override.
System.out.println ("running safely with 100kmph");
}

public static void main (String args[])


{
Honda honda = new Honda ();
honda.run();
}
}
Output: Compile-Time Error

In the above example, we have tried to override the final method in the
main() class. When we run the program, we will get a compilation error.

Final Class in Java


In Java, the final class cannot be inherited by another class. The main
purpose of using a class being declared as final is to prevent the class from
being subclasses.
If a class is marked as final, then no class can inherit any feature from
the final class. When an anonymous inner class is defined within the
body of a method, all variables declared final in the scope of that
method are accessible from within the inner class.
For scalar values, once it has been assigned, the value of the final
variable cannot change.
For object values, the reference cannot change.

Example to demonstrate Final Class in Class:


final class FinalClass
{
// create a final method
public void display ()
{
System.out.println ("This is a final method.");
}
}

class Main extends FinalClass


{
// try to override final method
public void display ()
{
System.out.println ("The final method is overridden.");
}

public static void main (String[]args)


{
Main obj = new Main ();
obj.display ();
}
}
Output: Compile-time Error

In the above example, we have created a final class named Final class.
Here, we have tried to inherit the final class from the Main class. When
we run the program, we will get a compilation error

Note:
• The final method can be inherited but you cannot override it.
• The blank Final Variable can be initialized only in the
constructor.
• Constructors cannot be declared as final because they cannot
be inherited.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy