×

Java Final Keyword

In Java, the last keyword is used to limit the user. The applications of the java final keyword have large range of usage in program development. Last can be:

  1. variable
  2. method
  3. class

A final variable with no value is known as a blank final variable or an uninitialized final variable. The variable of the program is also using the java final keyword that is final can be Implemented using the variables of the written code. Only in the function Object () {[native code]} is it possible to initialize it. The final blank variable may also be static, and it will only be initialized in the static block. We shall learn about these in detail. Learn the fundamentals of the final keyword first. The final keyword is a non-access modifier that prevents changes to classes, attributes, and methods (impossible to inherit or override).

When you want a variable, such as PI, to always store the same value, the final keyword is helpful (3.14159...). The phrase "modifier" refers to the last term.

Final variable in Java:

You cannot alter the value of a final variable once it has been set (It will be constant).

A final variable example:

We are going to update the value of the final variable speed limit; however it cannot be changed because final variables that have been given a value cannot ever be changed.

class Bicycle {
 final double spd_lmt=90.0;//final variable  
 void milage () {
  spd_lmt=300;
 }  
 public static void main (String s []) {
 Bicycle b=new Bicycle ();
b. milage ();
 }  
}//class is ended

Output:

Error due to compile time….

Java Final Method Concept

If is implemented to any method, you cannot override it, or we cannot make changes in the program as it is stored in static manner.

Example of final method:

class Bicycle {
  final void milage () 
{
System.out.println("running milage ");
}  
}  


class suzuki extends Bicycle {
   void milage ()
 {
System.out.println("300kmph is the ruuning safe limit");
}  
   public static void main (String s []) {
Suzukik= new Suzuki ();
k. milage ();
   }  
}  

Output:

Error because of compile time

Java Final Class

If you make any class as final, you cannot extend it because the class or method we declare is statically imported using final keyword.

Example of final class:

final class Bicycle {}  


class Suzuki1 extends Bicycle {
  void milage ()
{
System.out.println(" 300kmph is the speed limit of the bicycle");
}     
  public static void main (String s []) {
Suzuki1 S= new Suzuki1();
S. milage ();
  }  
}  

Output:

 Error due to compiling time.

Final Method Inherited Example

The inheritance can be occurred using the final method which is cannot be override in the program,

For Example:

class Bicycle {  
  final void milage ()
{
System.out.println("running milage...");
}  
}  
class Suzuki2 extends Bicycle {  
   public static void main (String s[]) {
    new Suzuki2(). milage ();
   }  
}  

Output:

Running mialge...

Initialize Blank Final Variable

Only in constructor we can initialize the final variable with empty declaration that is we declare nothing in the variable.

For example:

class Suzuki10{  
  final int spd_lmt;


  Bicycle10(){  
  speedlmt=140;
  System.out.println(speedlimit of the bicycle is given);
  }  


  public static void main (String s[])
{  
    new Bicycle10();
 }  
}  

Output:

140

Static Blank Final Variable

A static final variable is a keyword which stores the input value permanently and it is not initialized at the time of declaration which is known as static blank final variable. It can be initialized only in static block.

Example of static blank final variable:

Class Dattu {
  static final int info;
static{info=150;}  
  public static void main (String s []) {
    System.out.println(Dattu.info);
 }  
} 

We cannot change the value of variable ones the parameters are declared in the variable using final keyword.

class Bicycle11{
  int tube (final int d) {
d=d+2;//the value of the d cannot be changed as it is declared with the final keyword
d*d*d;
  }  
  public static void main (String s []) {
    Bicycle11 b=new Bicycle11();
b. tube (5);
 }  
}  

Output:

Due to compiling time Error is caused.

Related Topics

User Defined Custom Exceptions in Java

In this tutorial, we will discuss user-defined custom exceptions with examples. Introduction In Java, we have proactively characterised, Exception classes, for example, ArithmeticException, NullPointerException, ArrayoutOfBound and so on. These built-in exceptions are...

3 minutes read.

How to Change the Day in the Date using Java?

To operate with the date and time in Java, we need the Calendar abstract class. It provides a number of helpful interfaces that enable us to convert dates between a...

4 minutes read.

Java IO file not found exception

One of the exception classes offered by the java.io package is FileNotFoundException. An exception is raised when we attempt to access a file that isn't present in the system. It...

4 minutes read.

Highest precedence in Java

In Java, the operator is the first thing that springs to mind when discussing precedence. The order in which the operators in an expression are evaluated is controlled by a...

3 minutes read.

Program to find the duplicate characters in a string

Problem statement You have given with a string and your task is to find out the repeated characters from the string and print them. If no character is repeated, then you...

2 minutes read.

Series Program in Java

Series Program in Java The series program in Java is written to print the mathematical series such as the Fibonacci series, Pell series, etc. A few of the renowned series are...

12 minutes read.

Modules in Golang

Modules are a way to manage dependency versions and enable reproducible builds of Go programs. They were introduced in Go 1.11 and are now the recommended way to manage dependencies...

4 minutes read.

Prepared statement in Java

Prepared statement: A prepared statement is a statement that is pre-compiled SQL statement, and it is a sub-interface of a statement. Compared to other statement objects in java, Prepared Statement objects have...

3 minutes read.

How to take Multiple String Input in Java using Scanner class

Scanner class is a class which takes the multiple input data or the single input data through an objects and methods. The Scanner class will be available in the java.util...

3 minutes read.

if-else Program in Java

if-else Program in Java The if-else program in Java controls which code snippet will execute. The if-else program is very basic and yet very important. Because it checks how well one...

19 minutes read.

Concurrent Linked Deque in Java with Examples

Introduction Java's concurrent-linked deque, which holds its items as linked nodes, is unconstrained and thread-safe. Concurrent Linked Deque allows for element removal and addition on both sides because it implements the...

4 minutes read.

Java Stringjoiner Class

StringJoiner is a class which is used to construct a sequence of characters which are separated by a delimiter. Optionally, it starts with a provided prefix and ended with the...

5 minutes read.

Java Static Keyword

It can be either said that static declares the value to be the same, not only in the instance of a class but also as the whole. To declare a variable...

6 minutes read.

Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers. Tetranacci number Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends...

3 minutes read.

How to declare string array in Java

Introduction Array is a data structure with a fixed size, which allows us to store elements of similar type. Data of primitive types like int, char, float, string, etc. can be...

2 minutes read.

Java Naming Conventions

JAVA NAMING CONVENTIONS Java naming convention is a standard pattern for writing your identifier name such as class, interfaces, methods, constants, variable, etc. These patterns are not standard rules that you must...

2 minutes read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

How to Create an Immutable Class in Java

How to Create an Immutable Class in Java In Java, immutable means something that cannot be change. A class is called an immutable class if its content cannot be changed, once...

3 minutes read.