java Constants and final keyword

Java Constants and final keyword

In the Java programming language, constant data types are declared variable values ​​that, once declared or set in a program, cannot be modified or updated. Constant data types are commonly used to display and process fixed constant variable values. Once declared in a program, they are defined to remain unchanged throughout the program. The final keyword is used to declare or define constant data types and constant data variables in Java programs. The declared constant data cannot be modified after initialization in the program. Proper use of the final keyword in Java programs is crucial for writing good program code.

java Constants and final keyword

Final Keyword in Java Programming.

In Java programming, the final keyword can be used to declare a constant variable data type value, stop the Java method overriding process, and implement the multiple class inheritance process. When the final keyword is used with a variable, it ensures that the value of the declared variable cannot be modified after it is assigned in the program.

Uses of the final keyword in Java.

The final keyword with variables.

When the final keyword is applied to a variable in a Java program, this process makes the declared constant variable value immutable. So, after the variable is declared with the final keyword, its value cannot be modified in the program.

For reference variable objects in Java programs, the reference itself cannot be modified in the declaring program, while the value of the object to which the variable refers or points can still be modified. Unless the class of the declared object is immutable in nature.

final keyword with Java methods.

When the final keyword is applied to a method in a Java program, it blocks the existing method from being overridden in subclasses.

Final with Java Classes.

When the final keyword is applied to a Java program class, it blocks the class from being converted to a subclass in that program. In other words, Java programmers cannot create subclasses of a final class.

Constants data type in Java.

Constants data type variable values ​​in Java programs are typically represented using the final keyword. In a Java program, a Constants data type variable value is generally declared as static and final, meaning that the declared program variable value is shared and used by all instances of the class and cannot be modified within the program.

Declaring Constants in Java Using the Final Keyword.

Final variables declared in a Java program should generally be created in uppercase letters according to the Java Naming Conventions Conversion Rules and Regulations, and the variable word is separated by an underscore (_) symbol.

Example of defining the Constants data type in a Java program.

public class Main {

// let Declare here a constants variable

public static final double PI_VALUE = 3.14159; // Declare a constant value for the Pi_value variable with the final keyword

public static final int MAX_INPUT = 70; // Declare a constant variable for the maximum input allowe

public static void main(String[] args) {

System.out.println(“\n display the value of Pi – ” + PI_VALUE);

System.out.println(“Maximum input allowed – ” + MAX_INPUT);

// Uncommenting the line here displays a compile-time program error

// has PI_VALUE = 3.14; // Displays an error – cannot assign a value to a final keyword variable

}

}

About the declared constant data type.

  • static – In a Java program, the value of a user-declared constant data type variable is shared by all instances of the class.
  • final – Declaring the final keyword in a Java program means that the value of a constant variable cannot be modified after it has been initialized.

Remember, constant data types in Java programs are generally declared and processed at the class level, making them easily accessible throughout the program without creating an instance of the current class.

Using the final keyword with Java variables.

When Java programmers use the final keyword with program variables, it makes the declared program variable value constant in nature.

Example of a Java variable with the final keyword.

public class FinalVariableTest

{

public static void main(String[] args) {

final int MAX_LIMIT = 90; // Here a final keyword variable, declared as a constant nature type

System.out.println(“\n The maximum limit is – ” + MAX_LIMIT);

// Uncommenting the related line here will display a compile-time error due to an attempt constant modification

// MAX_LIMIT = 100; // It displays an error – here it cannot assign or change a value to a final declared variable

}

}

About the final constant keyword.

In any Java program, once a final variable is declared, the Java programmer cannot assign a new value to it.

  • For primitive data types – Here, the value of a primitive data type variable is locked in the program after it is declared. For example, int, double, etc.
  • For reference data types (objects) – This locks the reference to a Java program object. This means that Java programmers cannot point it to another program object. However, the internal state of the object declared in the program can still be modified, unless the object declared in the program is immutable.

Use of the final keyword with methods in Java.

When a class method is declared as a final data type in a Java program, it cannot be overridden by subclasses in the current program. This feature is used when Java developers want to ensure that the method’s behavior remains the same in the current program’s subclasses and cannot be modified by any type.

Example of a final method in a Java program.

class Root {

public final void display() {

System.out.println(“Let’s create a final method.”);

}

}

class Child extends Root {

// Uncommenting the code here will result in a program compile-time error.

// here public void display() { // Display Error – it cannot override a final class method from the Root class

// System.out.println(“Let’s create a final method.”);

// }

}

About the final keyword with methods.

Declaring a method as final in a Java program ensures that the program cannot override the method in any subclass.

The final keyword with methods in Java is used when a Java developer needs to provide a fixed implementation of a method in a base class and wants to prevent subclasses from modifying it.

Final Keyword with Java Classes.

When a class is declared final in a Java program, it cannot be extended by creating a new class. This means that no new subclasses can be created. This feature is used when Java developers want to prevent further subclassing of a class and fix it in the program so that the current program uses the class as it is.

Example of a Java final class.

final class Course {

public void running() {

System.out.println(“Course is running.”);

}

}

// Uncommenting the code here will result in a program compile-time error.

// class Java extends Course { // Display Error: Here Final class Course cannot be treated as subclassed.

// public void running() {

// System.out.println(“Course is running.”);

// }

// }

Detail explanation of final Keyword in Java

Keyword UsageEach keyword DescriptionWith Example
final with VariablesHere final keyword used to declare the variable’s value constant and unchangeable in all program.final int MAX_VALUE = 298;
final with MethodsFinal with method used to block a method from being overridden by subclasses in active program.public final void display() {}
final with ClassesFinal with class used to restrict a class from being subclassed with multiple subclass in blocks.public final class TestClass {}
final with Local VariablesIt used to Ensures that a local variable is assigned a value only once in active program.final int data = 40;
final with ArraysFinal with array used to restrict reassignment of the array reference element, but the array’s contents can be modified with reference according to need.final int[] array = {7, 3, 8,4,1};

Some features of a Java final class.

  • A class declared as final in a Java program cannot be subclassed.
  • This feature is useful for security reasons or to prevent any modifications to classes that have already been tested or optimized in the proper order in Java.

Leave a Reply