Final Keyword in Java - PowerPoint PPT Presentation

About This Presentation
Title:

Final Keyword in Java

Description:

– PowerPoint PPT presentation

Number of Views:1
Slides: 8
Provided by: dataflair
Category:
Tags:

less

Transcript and Presenter's Notes

Title: Final Keyword in Java


1
FINAL KEYWORD IN JAVA
In this article, we will discuss Final Keyword In
Java. There are around 46 keywords in Java. Each
keyword has a special purpose that fulfills the
need of the programmer. The final keyword in Java
also plays an indispensable role. You can apply
the final keyword with a variable, method, or
even in a class. This article depicts the various
ways to use the final keyword in java
programming.
2
PURPOSE OF JAVA FINAL KEYWORD
The Final keyword mainly restricts the user from
performing specific actions. It makes them
helpless to change the given value. In other
words, it is a non-access specifier that
restricts the value of a variable from being
changed. So, what happens to a method with a
final keyword? The methods with a final keyword
cannot be overridden by other subclasses. The
keyword is final to a class that puts a full stop
to its inheritance property. No other class can
extend or inherit a final class.
3
FINAL VARIABLE IN JAVA
A final variable is usually a constant as it acts
as one whose value cannot be modified. This is
applicable in the case where we have only normal
variables. If it is a reference variable, we
should keep in mind that there is a restriction.
This restriction with the final reference
variable states that there is no possibility for
us to change What the object is referring to.
In contrast, we can change the object itself,
which is not considered re-assigning. It is a
property of the final keyword known as
non-transitivity.
4
FINAL VARIABLE INITIALISATION IN JAVA
As initializing a final variable in java is
essential, here are the ways to do it 1. An easy
and simple way to initialize a final variable is
during its declaration. When a variable is not
declared, it is a blank final variable. 2. A
blank final variable can be initialized inside an
instance-initialiser block or the classs
constructor. If there are multiple constructors
in the class, we must initialise the final
variable in every constructor to avoid errors. 3.
The other way to initialise the final static
variable is inside a static block.
5
WHEN TO USE JAVA FINAL VARIABLE?
The only difference between a normal and a final
variable is that we cannot alter the latters
value. A value, once assigned, is fixed for a
final variable, whereas this does not imply a
normal variable. Therefore, it is clear that we
must use a final variable only when we do not
want a value to be constant throughout the
execution of the program.
6
PURPOSE OF JAVA FINAL METHODS
The final method in Java prevents the improper
usage of the methods definition while
overriding. It is not logically improper. The
final method aims to prevent misinterpretation by
the reader. Therefore, we should avoid the
unwanted method definitions and declare them as
final. Suppose we create a class Animal and a
non-final method eat() in it. Assume that another
programmer creates a class Goat and includes and
overrides the eat() method. This might lead to
misunderstanding and printing the wrong food name
for Goat as Meat or Dear, etc.
7
CONCLUSION
I hope you are clear about implementing the final
keyword with a variable method and a class. The
Final keyword acts accordingly to the place where
it is used. You can try out these sample programs
to clearly understand their work.
Write a Comment
User Comments (0)
About PowerShow.com