Discover how to create immutable class in java, include the articles, news, trends, analysis and practical advice about how to create immutable class in java on alibabacloud.com
and LastName of the name of the object. * return NewName1 (Name.getfirstname (), Name.getlastname ()); $ }Panax Notoginseng Public Static voidMain (string[] args) - { theName1 n =NewName1 ("Goku", "Sun")); +Person1 p =NewPerson1 (n); A //The firstname value for the name of the person object is "Goku" the System.out.println (P.getname (). Getfirstname ()); + System.out.println (P.getname (). Getlastname ()); - //change the name of the person object to the Fir
Why is a Java string an immutable object? (1)
This article mainly introduces immutable objects in Java and immutable String classes in Java. Why is the Java String
; }}classperson{Privatefinal name name; PublicPerson (name name) { This. name=name; } PublicName GetName () {returnname; } @Override PublicString toString () {returnName.getfirstname () +"$"+Name.getlastname (); } }The person class contains a mutable type name, although name is decorated with final and private and only getter, but the value of name can still be modified: Person person= Thenew person (new Name ("Hello", "World"));
What is an immutable class
When you obtain a reference to this class, you cannot change the contents of this class;
Once an instance of an immutable class is created, its member variables cannot be modified;
One, Java provides three kinds of ClassLoader to load class:1.BootStrap ClassLoader: called the Startup class loader, is the topmost class loader in the Java class loading hierarchy, responsible for loading the core
way to change the fields in an object.
Define all fields as final and private
Instead of allowing subclasses to override methods in a class, the simplest way is to declare the class as final, and a more complex method is to define the constructor as private and use the factory method when creating the instance.
If the instance fields contain referenced objects, do not change them: (1) do not provi
the initialization method does not imply that the object will be generated, and that the new keyword in your Java code, along with the call to the constructor, produces only one object whose parent object is not generated, so it is perfectly reasonable to call the parent class as the constructor of the abstract class. And the initialization method for the virtua
First, create the class:Test.java// Defining Classes Public class test{ // attribute String name; String gender; int Age ; // method, no parameter no return Public void eat () { System.out.println ("I'm eating.") "); }}Second, the use of the class:If the class is in the same directory as the file that uses the
Class Name Object name =new class name ();Class Name Object name This explanation is to create a class name class reference variable named "Object name"New; The explanation for this is the instantiation of the objectThe explanat
new instance is returned. At first, immutable types may seem unnatural, but they have many advantages over their corresponding mutable types. Non-mutable types are easier to design, implement, and use; they are less likely to make errors and are more secure [J, 13].
To perform computation on a variable that contains an immutable object reference, we need to assign the computation result to the variable. In
1. Requirement of String Pool
String pool (String intern pool) is a special storage area in Method Area. when a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference.
The following code will create only one string object in the heap.
String string1 = "abcd";String string2 = "abcd";
Here is how it looks:Private in
In Java, how can I encapsulate my own classes and create and use my own class libraries? -- Thank you for reading this article. For more information, see the source!
With the accumulation of programming experience, you will find that the classes you write in some projects are often used multiple times in other projects. You will surely think: How do you accumulat
There are two ways to create a Thread in Java: using the Thread class and using the Runnable interface. You must create a Thread instance when using the Runnable interface. Therefore, both the Thread class and Runnable interface must be used to
Use the java. util. concurrent. ThreadFactory class to create a thread
The factory design pattern is one of the most common design patterns in Java. It is a creation-type design pattern that can be used to create the objects required by one or more classes. With this factory
There are two ways to create threads in Java: Using the thread class and using the Runnable interface. You need to create a thread instance when using the Runnable interface. Therefore, whether you create a thread through the thread clas
Blogjava
There are two ways to create a Thread in Java: using the Thread class and using the Runnable interface. You must create a Thread instance when using the Runnable interface. Therefore, both the Thread class and Runnable interface must be used to
After creating a new directory in the project, to add a Java class file to the directory, right-click New finds no corresponding option.Cause: After creating the directory, you need to set the directory role so that idea can be identified.Method: File-project StructureThe following are explained for several directory types:
Sources: Generally used for labeling similar src to such a compiled directory. S
ZC: Brief excerpt:“1. The object class out creates the JVM.You must create a JVM environment before you use Java classes. The JDK is done by Java.exe. This article has the static method of the object class BEGINJVM to create, with the ENDJVM to close.After the JVM is created
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.