Last Updated:

Final Class in Java - What mean it?

In this article, we will explore the use of the final keyword. you can use the final keyword with variables, methods, and classes. We'll take a closer look at the following topics:

  • finite variable
  • final method
  • final class

What is the final class in Java?

 

A class declared with the final keyword is called a finite class. A finite class cannot be inherited by subclasses. By using a finite class, we can restrict class inheritance. We can only create a class as a finite if it is complete in nature, which means that it doesn't have to be abstract. In java, all wrapper classes are finite classes, such as String, Integer, and so on.

If we try to inherit the target class, the compiler will throw an error at compile time. We can't create a class as immutable without a finite.

How do I create one?

We can create the final class using the final keyword during the class declaration. We need the classes to be complete. It doesn't have to be an abstract class.

accessModifier final class className{
// Teloclass

}

Here, accessModifier specifies the scope of the method. You can read about access modifiers.
final is the keyword that is used to create the final method. Finite variables are nothing more than constants. We cannot change the value of the target variable after it is initialized. Let's take a look at the code below:

Final Class in Java

Exit:

Final Class in Java

We got a compilation error in the above program because we tried to change the value of the final variable "MAX_VALUE".

Note: It is considered good practice to have permanent names in UPPERCASE (UPPERCASE).

Empty end variable

 

A target variable that is not initialized during the declaration is called an empty destination variable. We must initialize an empty target variable in the class constructor, or it will throw a compilation error (Error: Variable MAX_VALUE may not have been initialized).

Here's how an empty end variable is used in a class:

Final Class in Java

Exit:

100

What is the use of an empty finite variable?

 

Let's say we have a student class that has a field called "No." Since RollNo doesn't have to change after a student registers, we can declare it as the final variable in the class, but we can't initialize rollno in advance for all students (otherwise all students would have the same rollno). In this case, we can declare the rollno variable to be an empty target and initialize this value during object creation as follows:

Final Class in Java

Exit:

Rollno is:1234

Uninitialized static end variable

A static final variable that is not initialized during the declaration can only be initialized in a static block. Example:

Final Class in Java

Exit:

1230

Окончательный метод (final method)

The final method cannot be overridden. This means that even though a subclass can call the final method of the parent class without any problems, it cannot override it.
Example:

Final Class in Java

The above program will throw a compilation error, but we can use the parent class's final method in the subclass without any problems. Let's take a look at this code: This program will work fine, since we are not overriding the final method. This shows that finite methods are inherited, but they do not have the right to override.

Final Class in Java

Exit:

XYZ Class Method

Final class

We can't expand the last class. Consider the following example:

Final Class in Java

Exit:

ThetypeABCcannotsubclassthefinalclassXYZ

Moments to remember

  1. The constructor cannot be declared final.
  2. The local destination variable must be initialized during the declaration.
  3. All variables declared in the interface are final by default.
  4. We cannot change the value of the final variable.
  5. The final method cannot be overridden.
  6. The final class is not inherited.
  7. If the parameters of the method are declared final, the value of these parameters cannot be changed.
  8. It is recommended to name the final variable in all CAPITAL LETTERS.
  9. final, finally, and finalize are three different terms. finally is used in exception handling, and finalize is the method called by the JVM during garbage collection.