How many immutable classes in java




















Next Topic Understanding toString method. Reinforcement Learning. R Programming. React Native. Python Design Patterns. Python Pillow. Python Turtle. Verbal Ability. Interview Questions. Company Questions. Artificial Intelligence. Cloud Computing. Data Science. Angular 7. Join For Free. Steps to create Immutable class in java Make your class final, so that no other classes can extend it. Make all fields private so that direct access is not allowed.

Special attention when having mutable instance variables Inside the constructor, make sure to use a clone copy of the passed argument and never set your mutable field to the real instance passed through the constructor.

Make sure to always return a clone copy of the field and never return the real object instance. Implementation of Immutable class in java Java.

Opinions expressed by DZone contributors are their own. Java Partner Resources. In the example given below, I have a class called Fruits inside there is a List. I have made the class immutable by making the List private and final as well as there is no setter provided.

If I am instantiating an object of Fruit, the constructor will be given a list. Client program does have a reference of this List already client side and hence the List can be modified easily and hence the immutability of the class will be lost. To address this problem , I am creating a new List in the constructor which copies all the values supplied by the client.

Now if the client adds more values in the list, the external reference will get affected however, I am not storing that external reference anymore in my immutable class. This can be verified by overriding an hashcode in the immutable class. No matter how many times the client modifies the list the hashcode of my immutable class object will remain unchanged, as the list it accepts is no more pointing to the external list.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Examples of immutable classes Ask Question. Asked 10 years, 8 months ago. Active 1 year, 9 months ago. Viewed 98k times. I already know the definition of immutable classes but I need a few examples. Improve this question.

Kris String is a good example. Are there any other immutable classes? All answers below expect one or two explains how to create Immutable class or says what is immutable rather than answering the actual question asked. Add a comment. Active Oldest Votes. Some famous immutable classes in the Standard API: java.

String already mentioned The wrapper classes for the primitive types: java. Integer, java. Byte, java. Character, java. Short, java. Boolean, java.

Long, java. Double, java. Float java. StackTraceElement used in building exception stacktraces Most enum classes are immutable, but this in fact depends on the concrete case. BigDecimal at least objects of those classes themselves, subclasses could introduce mutability, though this is not a good idea java. Font - representing a font for drawing text on the screen there may be some mutable subclasses, but this would certainly not be useful java.

BasicStroke - a helper object for drawing lines on graphic contexts java. Color - at least objects of this class, some subclasses may be mutable or depending on some external factors like system colors , and most other implementations of java.

Paint like java. GradientPaint, java. LinearGradientPaint java. RadialGradientPaint, I'm not sure about java. TexturePaint java. Cursor - representing the bitmap for the mouse cursor here too, some subclasses may be mutable or depending on outer factors java. Locale - representing a specific geographical, political, or cultural region. UUID - a as much as possible globally unique identifier while most collections are mutable, there are some wrapper methods in the java.

URL and java. URI - representing a resource on the internet or somewhere else java. Inet4Address and java. Inet6Address, java. InetSocketAddress most subclasses of java. Permission representing permissions needed for some action or given to some code , but not java. PermissionCollection and subclasses. All classes of java. Most of the classes of the subpackages of java. The same is valid for all other classes which can't have objects instances , most famously: java.

Void java. System but this has some mutable static state - in , out , err java.



0コメント

  • 1000 / 1000