×

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

Getting the Day from the Date in Java?

To extract the day's name from the date, we will write Java application. When dealing the date and time in Java, the following classes are used. Class for Calendars: This class is...

6 minutes read.

Java Integer toHexString() method

The toHexString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 16. Syntax public static String toHexString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Java Command not found

Java Command not found error is displayed if Java is not installed on the computer or if the command prompt cannot find Java.exe to run the program. Our Java software...

3 minutes read.

Sorting a String in Java

Sorting a String in Java Sorting is a process of arranging available data objects in a meaningful format that is ascending or descending order. Sorting a string or array of String...

4 minutes read.

Types of Sockets in Java

The fundamental idea behind Java's networking capability is the socket. Early in the 1980s, the Berkeley UNIX 4.2BSD version included the socket paradigm. Berkeley socket is the term employed as...

9 minutes read.

Thread Safety and How to Achieve it in Java

Before diving into the topic, let’s just recap the concept of Multithreading provided by Java where we can create and execute multiple threads of the same object. When these multiple...

5 minutes read.

Java New Keyword

To create a class instance in Java, use the new keyword. In other words, it returns a reference to the memory that was allocated for a new object and instantiates...

3 minutes read.

Java Integer toUnsignedString() method

The toUnsignedString() method of Java Integer class returns a string representation of the argument as an unsigned decimal value. The second syntax returns a string representation of the given argument as...

2 minutes read.

Method Overloading In Java

If the same class consists of different methods with the same name and the methods can vary by different number of parameters then it is known as Method Overloading. Method...

2 minutes read.

Unicode System in Java

In this tutorial, we will understand the meaning and emergence of the Unicode system in java. The Unicode system is one of the important and useful features in the world...

6 minutes read.

Java Math addExact() Method

The addExact() method of Math class returns the sum of the two arguments, throwing an exception if the result overflows a long or an int. Syntax public static int addExact (int x,...

1 minute read.

Why we use static in Java

Many reserved keywords in Java cannot be used as variable names or identifiers. Java uses static keywords frequently because of its effective memory management system. In most cases, you must...

3 minutes read.

Java Garbage Collection Interview Questions

One of the key areas of Java is garbage collection. Garbage collection enables apps to manage memory automatically. Interviewers frequently ask inquiries about garbage collection. Q1: What is the purpose of...

6 minutes read.

Array and String with Examples in Java

Array in Java: An array in Java is a group of variables with similar types that have a common name. The arrays used in Java differ from those used in C/C++. Key...

6 minutes read.

Java Generics Questions

Introduction We'll walk through a few real-world examples of interview questions and responses for Java generics in this article. Java 5 saw the debut of the fundamental idea of generics. Due to...

9 minutes read.

What is advance Java?

The definition of advance inside the dictionary refers to a forward motion, development, or progress, and the definition of enhancing is something that improves things. To become experts in that...

3 minutes read.

Snake and Ladder Problem in Java

Find the smallest number of dice throws necessary to reach the destination or last cell from the source or first cell on a snake and ladder board. Essentially, the player...

6 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

Delete a Cycle from a Linked List in Java

Assume a method detectAndRemoveLoop(), which examines if a given Linked List includes a loop and, if so, eliminates this Loop and returns true. This returns false if the list does...

7 minutes read.

Java Integer reverseByte() method

The reverseByte() method of Java Integer class returns the value obtained by reversing the order of the bytes in the 2’s complement binary representation. Syntax public static int reverseByte (int i) Parameters The parameter...

1 minute read.