Create an immutable class with JAVA

What is immutable

Immutable means ** immutable **. In other words, an immutable class is a class whose instance contents do not change (= cannot be rewritten). Rewriting an immudatable class means recreating an instance (be careful when using it because old instances will accumulate and the processing speed will slow down if instances are created many times during iterative processing. I have to). When using an immutable class, it is necessary to check whether the contents have been changed and whether the reference type value is used.

Benefits of immutable

Since the state of immutable objects does not change from the time of creation, there is no concern that the value will change unintentionally during processing. A typical immutable class is the String class.

Conditions at the time of creation

To create an immutable class, the following four conditions must be met.

** 1. Declare the class as final **

** 2. Make all fields final and private ** Declare the class as final, or make the constructor private and instantiate it with a factory method

** 3. Do not define setter ** Declare the class as final, or make the constructor private and instantiate it with a factory method

** 4. The field does not contain a reference to a mmutable object **

Fruits.java


public final class Fruits {
    private final String color;
    private final int amount;

    public Fruits(String name, String, int age) {
        this.name = color;
        this.age = amount;
    }

    public String getColor() {
        return color;
    }

    public int getAmount() {
        return amount;
    }
}

Attempting to change will result in an error.

FruitsTest.java


@Test
public void fruitsTest() {
    final Person Fruits = new Fruits("Red", 28);
    // error
    // fruits.setColor("yellow");
    // fruits.amount(30);
}

Recommended Posts

Create an immutable class with JAVA
[Java] Create an executable module with Gradle
Create an app with Spring Boot 2
Create an excel file with poi
Create an app with Spring Boot
Run an application made with Java8 with Java6
I can't create a Java class with a specific name in IntelliJ
Let's create an instance with .new yourself. .. ..
Create an infinite scroll with Infinite Scroll and kaminari
Create a temporary class with new Object () {}
Create an or search function with Ransack.
Create Java applications with IBM Cloud Functions
[Java] Get the date with the LocalDateTime class
Java class methods
[Java] Class inheritance
java Scanner class
Java HashMap class
java (abstract class)
[Java] Nested class
Java anonymous class
About Java class
[java] abstract class
[Java] Object class
Java local class
Create a CSR with extended information in Java
Create a simple bulletin board with Java + MySQL
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
Let's create a timed process with Java Timer! !!
Create an RSA encryption-enabled JSON API with wicket
[Java] Create a collection with only one element
Build an E2E test environment with Selenium (Java)
[Java] Find prime numbers with an Eratosthenes sieve
Create an EC site with Rails5 ⑤ ~ Customer model ~
Create an Annotator that uses kuromoji with NLP4J [007]
[Swift] Create an image selection UI with PhotoKit
Create an EC site with Rails 5 ⑩ ~ Create an order function ~
Create an ARM-cpu environment with qemu on mac and run java [Result → Failure]
Install java with Homebrew
[Java] Sort ArrayList with elements of your own class
Try create with Trailblazer
About class division (Java)
[Java] Find prime numbers with an Eratosthenes sieve (Part 2)
About Java StringBuilder class
Change seats with java
Install Java with Ansible
Create an HTTPS file server for development with ring-jetty-adapter
Create an EC site with Rails5 ⑦ ~ Address, Genre model ~
[Java] Create a filter
Create an EC site with Rails 5 ⑨ ~ Create a cart function ~
[Java] About Singleton Class
[java] throw an exception
[Beginner] Create a competitive game with basic Java knowledge
Create an EC site with Rails5 ④ ~ Header and footer ~
Comfortable download with JAVA
Create an E2E test environment with Docker x Cypress
Java inner class review
Create a user with an empty password on CentOS7
Switch java with direnv
Create an EC site with Rails5 ⑥ ~ seed data input ~
[Java] Get the date 10 days later with the Calendar class
Java class type field