• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Language » Final Keyword in java
Next →
← Prev

Final Keyword in java

By Dinesh Thakur

There are times when you want to prevent inheritance. Perhaps there is no need the extensibility for various reasons. If you know this for sure, there are advantages to declaring the final keyword precisely that.

Java has a special keyword called final that can use for creating final methods, final classes, and final variables. The final modifier is void, which simply means that no value is returned.The final modifier indicates that the value of this member cannot change. We get a compile-time error if our program tries to change its contents. Each of these is explained in more detail next.

We’ll be covering the following topics in this tutorial:

  • Final Methods
  • Final Classes
  • Final Variables

Final Methods

Final methods are methods whose implementations cannot be changed. When a method declared as final, it cannot be overridden by a subclass method. In private methods is equal to final, but in variables, it is not. For example, consider a class E that contains a final method called computeSquare that computes the square of its argument:

public class E {
    public final int computeSquare(int x) {
         return x*x;
    }
}

By declaring computeSquare as fina1, we ensure that it not be overridden in a subclass. Thus, if subclass F attempts to override computeSquare, it causes a compilation error:

public class F extends E {
       // error: cannot override the final method computeSquare in E
       public int computeSquare(int x) {
              // some code goes here
       }
}

Final Classes

There are times when you want to prevent inheritance. Perhaps there is no need to extend the class, for instance. If you know this for sure, there are advantages to declaring this class final.

Just like final methods, a class can also be declared final. A final class is a class that cannot use as a superclass (i.e., a Class cannot extend final class). To declare the class as final, precede the class header with the final keyword. So the class header looks like below.

final class ClassName {
…………..
}

When you declare a class to be final, all methods of this class are implicitly final. It is because another class cannot extend the final class so no class can be in a position to override any of the methods in the final class. For example, String and StringBuffer classes are examples of final classes.

public final class HourlyEmployee extends Employee {
     // class definition
}

A class can declare as final for either of the following two reasons:

1. None of the methods in the class should override.
2. The class should not extend further.

The two main advantages of declaring final classes are higher efficiency and safety. The dynamic binding inherent in virtual methods is harder on the compiler, resulting in slower execution. By declaring a class final, virtual methods are replaced by the compiler with in-line code, speeding up the process. Final classes are also safer, in that they avoid ambiguous situations where a message sent may end up at an object that returns the wrong data.

Examples of final classes in the Java API are:

• An array is a collection of similar elements. The array is a predefined class present in java.lang package. It is a final class which can’t be inherited.

• In java String is a predefined class present in java.lang package. A string is a final class, and it is immutable.

• Each of the numeric type-wrapper classes – Byte, Short, Integer, Long, Float, and Double extends class Number. Also, the type-wrapper classes are final classes, so you cannot extend them.

• The System class provides a system-independent programming interface for accessing such resources outside the Java environment. The System class should not instantiate. It is a final class, and all its variables and methods are static.

• The java.sql.Numeric class adds the capability of representing SQL fixed point NUMERIC and DECIMAL values to the java.lang.Number class. The Numeric class is a final class and, therefore, cannot be redefined.

Final Variables

Final variables are declared using the final keyword. A final variable can be assigned a value only once. A variable that is declared as final and not initialized is called a final blank variable. A final blank variable forces the constructors to initialize it. Java local classes can only reference local variables and parameters that declared as final. It declares a variable called ANGLE:
final int ANGLE = 10;
Reassigning to ANGLE again results in an error:
ANGLE = 20; // error, ANGLE has been already assigned.
Final variables are useful in local and anonymous classes.

How do final variables differ from constants? Constants are class variables because they declared with the static modifier. It means that a single copy of this variable shared among all instances of a class. Final variables, on the other hand, are instance variables.

A distinct advantage of declaring a java variable as static final is the compiled java class results in faster performance.

‘final’ should not call as constants. Because when an array declared as final, the state of the object stored in the array can be modified. You need to make it immutable in order not to allow modifications. In a general context, constants do not allow modifying. In C++, an array declared as const not allow the above scenario, but java allows. So java’s final is not the general constant used across in computer languages.

A variable that is declared static final is closer to constants in general software terminology. You must instantiate the variable when you declare it static final.

You’ll also like:

  1. Final Keyword in Java
  2. What is the Use of ‘FINAL’ Keyword?
  3. What is Java keyword (reserved words)? – Definition
  4. Final Method in Java with Example
  5. Final Variable in Java Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW