Immutable classes in Java

Source: Internet
Author: User

package com.home;public class Address {private final String detail;public Address() {this.detail = "";}public Address(String detail) {this.detail = detail;}public String getDetail() {return detail;}@Overridepublic int hashCode() {return detail.hashCode();}@Overridepublic boolean equals(Object obj) {if (obj instanceof Address) {Address address = (Address) obj;if (this.getDetail().equals(address.getDetail())) {return true;}}return false;}}



Package com. home; public class Person {private final Name name; public Person (Name name) {super (); this. name = name;} public Name getName () {return name;} public static void main (String [] args) {Name n = new Name ("3 ", "Zhang"); Person p = new Person (n); System. out. println (p. getName (). getFirstName (); // change the firstName attribute value n of the Person object Name attribute. setFirstName ("nothing"); System. out. println (p. getName (). getFirstName ());}}


Name:

package com.home;public class Name {private String firstName;private String lastName;public Name() {super();}public Name(String firstName, String lastName) {super();this.firstName = firstName;this.lastName = lastName;}public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}}


Package com. home; public class Person {private final Name name; public Person (Name name) {super (); // set the name attribute to the Name object temporarily created, the firstName and lastName attributes of this object // they are the same as the firstName and lastName attributes of the passed name object. this. name = new Name (name. getFirstName (), name. getLastName ();} public Name getName () {// returns an anonymous object, the firstName and lastName attributes of this object // they are the same as the firstName and lastName attributes of the name attribute in this object. return new Name (name. getFirstName (), name. getLastName ();} public static void main (String [] args) {Name n = new Name ("3", "Zhang "); person p = new Person (n); System. out. println (p. getName (). getFirstName (); // change the firstName attribute value n of the Person object Name attribute. setFirstName ("nothing"); System. out. println (p. getName (). getFirstName ());}}


Package com. home; public class CacheImmutale {private final String name; private static CacheImmutale [] cache = new CacheImmutale [10]; private static int pos = 0; public CacheImmutale (String name) {super (); this. name = name;} public String getName () {return name;} public static CacheImmutale valueOf (String name) {// traverses cached objects for (int I = 0; I <pos; I ++) {// if the same instance already exists, the cached instance if (cache [I]! = Null & cache [I]. getName (). equals (name) {return cache [I] ;}// if the buffer pool is full if (pos = 10) {// overwrite the first cached object to cache [0] = new CacheImmutale (name); pos = 1; return cache [0];} else {// cache the newly created object, pos plus 1 cache [pos ++] = new CacheImmutale (name); return cache [pos-1];} @ Overridepublic int hashCode () {return name. hashCode () ;}@ Overridepublic boolean equals (Object obj) {if (obj instanceof CacheImmutale) {CacheImmutale ci = (CacheImmutale) obj; if (name. equals (ci. getName () {return true;} return false;} public static void main (String [] args) {CacheImmutale c1 = CacheImmutale. valueOf ("hello"); CacheImmutale c2 = CacheImmutale. valueOf ("hello"); System. out. println (c1 = c2); // The output result is true }}


Package com. home; public class Person {private final Name name; public Person (Name name) {super (); // set the name attribute to the Name object temporarily created, the firstName and lastName attributes of this object // they are the same as the firstName and lastName attributes of the passed name object. this. name = new Name (name. getFirstName (), name. getLastName ();} public Name getName () {// return the name attribute of the current instance to return name;} public static void main (String [] args) {Name n = new Name ("3", "Zhang"); Person p = new Person (n); System. out. println (p. getName () + "" + p. getName (). getFirstName (); // change the firstName attribute value n of the Person object Name attribute. setFirstName ("nothing"); System. out. println (p. getName () + "" + p. getName (). getFirstName ());}}



Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.