top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Can somebody explain immutable classes in JAVA

+4 votes
328 views
Can somebody explain immutable classes in JAVA
posted Jan 18, 2014 by Neeraj Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Immutable objects are simply objects whose state (the object's data) cannot change after construction. Examples of immutable objects from the JDK include String and Integer.

Guidelines to make a class immutable:
1) Don’t provide “setter” methods — methods that modify fields or objects referred to by fields.
2) Make all fields final and private
3) Don’t allow subclasses to override methods
4) Special attention when having mutable instance variables

answer Jan 19, 2014 by Atul Mishra
...