Home > Java > javaTutorial > How are final objects created in Java?

How are final objects created in Java?

WBOY
Release: 2024-04-11 14:00:02
Original
822 people have browsed it

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;
    }

}
Copy after login

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;
    }

}
Copy after login

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"
Copy after login

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;
    }

}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template