Java - Final Modifier

Java Conceptuel Diagram

About

Final is modifier prohibiting value modification.

Type

Class

Declaring an entire class final prevents the class from being subclassed.

This is particularly useful, for example, when creating an immutable class like the String class.

An interface cannot be final

Method

You use the final modifier in a method declaration to indicate that the method cannot be overridden by subclasses.

You might wish to make a method final if it has an implementation that should not be changed and it is critical to the consistent state of the object.

Example of using a final method for initializing an instance variable:

class Whatever {
    private varType myVar = initializeInstanceVariable();
	
    protected final varType initializeInstanceVariable() {

        //initialization code goes here
    }
}

This is especially useful if subclasses might want to reuse the initialization method. The method is final because calling non-final methods during instance initialization can cause problems.

Methods called from constructors should generally be declared final. If a constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results.

Member

The modifier final indicate that the value of a member will never change

The final modifier indicates that the value of a field cannot change.

The reference to final variable is thread safe since it can never be changed.

A final member can be initialized through:

  • an Initializer expression
final int myFinalValue = 5;
final int myFinalValue = myFunction();

protected final int  initializeInstanceVariable() {

        // initialization code goes here
        myFinalValue = 5;
        
}





Discover More
Card Puncher Data Processing
Data Type - Boxing (Autoboxing|Autowrapping) and Primitive Wrapper

Boxing and Unboxing are the terms used to describe automatically wrapping a primitive type in a wrapper class and unwrapping it as necessary. This operation has the advantage to add basic method functions...
Java Conceptuel Diagram
Java - (Method|Functions)

A function that belong to an object is called a methods. A static method is then a sort of function. Methods: operate on an object's internal state and serve as the primary mechanism for object-to-object...
Simple Class
Java - Class (Definition)

A java/lang/Classclass provides the blueprint for objects; you create an object from a class. All classes are derived from the Object class. A class declaration names the class and encloses the class...
Java Conceptuel Diagram
Java - Constant

The names of constant fields are in upper-case letters. By convention, the names of constant values are spelled in uppercase letters. If the name is composed of more than one word, the words are separated...
Java Conceptuel Diagram
Java - Modifier

A modifiers affect runtime behaviour of class or method Access modifiers: public, protected, and private Modifier requiring override: abstract Modifier restricting to one instance: static Modifier...
Java Conceptuel Diagram
Java Concurrency - Immutable Object

Immutable object in Java Concurrency. Don't provide “setter” methods — methods that modify fields or objects referred to by fields. Make all fields final and private. Don't allow subclasses...
Java Conceptuel Diagram
Java Concurrency - Synchronization (Thread Safety)

in java. Java offers two basic synchronization idioms: synchronized methods synchronized statements final fields, which cannot be modified after the object is constructed, can be safely read...



Share this page:
Follow us:
Task Runner