search
HomeJavajavaTutorialHow are final objects created in Java?

There are two ways to create final objects in Java: declare a final variable or declare a class using the final modifier. When you declare a final variable, the object is created through an initializer; when you declare a final class, the class instance is immutable. Importantly, references to final objects can still change, but the objects they point to are immutable.

How are final objects created in Java?

How to create final objects in Java

In Java, the final keyword is used to declare immutable objects. This means that once a final object is created, it is impossible to change its state. This is useful for creating thread-safe classes and preventing accidental modification of sensitive data.

There are two main ways to create a final object:

1. Declare a final variable

The simplest way is to declare a final variable and Initialized as an object:

public class FinalObjectExample {

    private final String name;

    public FinalObjectExample(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

}

2. Use the final modifier to declare the class

Another method is to use the final modifier to declare the entire class. This means that instances of the class are immutable:

public final class ImmutableClass {

    private final String value;

    public ImmutableClass(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

}

The important thing to remember is that a reference to a final object can still be mutable itself. This means that you can assign a reference to a final object to a different variable:

FinalObjectExample obj1 = new FinalObjectExample("John");
FinalObjectExample obj2 = obj1;

obj2.getName(); // 返回 "John"

However, you still cannot modify the object pointed to by the obj1 or obj2 reference.

Practical case

Consider a class used to store sensitive data:

public class SensitiveData {

    private final int id;
    private final String value;

    public SensitiveData(int id, String value) {
        this.id = id;
        this.value = value;
    }

    public int getId() {
        return id;
    }

    public String getValue() {
        return value;
    }

}

By declaring the class final, we can ensure that the data is created remains unchanged, thereby improving security.

The above is the detailed content of How are final objects created in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.