search
HomeJavaJavaBasejava mutable classes and immutable classes

java mutable classes and immutable classes

java mutable classes and immutable classes

a) Mutable classes

After obtaining an instance of this class, you can change the content of this instance, such as changing the internal member variables of this instance.

b) Immutable class

After obtaining an instance of the class, the content of this instance cannot be changed. Once an immutable instance is created, the value of its internal member variables It can no longer change.

Immutable classes are typically like String. Once a String object is created, it cannot be changed. We change the value of the String object in the foreground. In fact, a new String object is created in the memory, and the original object remains unchanged. Change.

StringBuild is a mutable class, because every modification to its object affects the object itself and does not create a new object.

Characteristics of immutable classes:

All members are private final

Does not provide methods to change members, member variable assignment is generally done in the construction Assign a value in a function.

Ensure that all methods will not be overloaded: use final to define the class, or add final to all methods of the class.

If a class member is not a primitive variable or an immutable class, it must Ensure the immutability of the class by using the deep clone method during member initialization or get method.

php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!

The above is the detailed content of java mutable classes and immutable classes. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version