How to create Immutable class?There are many immutable classes like String, Boolean, Byte, Short, Integer, Long, Float, Double etc. In short, all the wrapper classes and String class is immutable. We can also create immutable class by creating final class that have final data members as the example given below: Example to create Immutable classIn this example, we have created a final class named Employee. It have one final datamember, a parameterized constructor and getter method. ImmutableDemo.java Output: Pancard Number: ABC123 The above class is immutable because:
These points makes this class as immutable. Next TopicUnderstanding toString() method |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India