Please explain how to make a java class as Immutable?

Dear Experts,

Please give me an example, how to make a java class as Immutable,
haneef_nbAsked:
Who is Participating?
 
Gurvinder Pal SinghConnect With a Mentor Commented:
there is no member variable inside, and their is no method declaration.

Actually, there is no need for the JVM to know if it is marker interface or not.

Only purpose of such an interface is for a program to find out if a class is implementing this interface or not. So, it just mark a class. For example, if your class implements clonable interface, it tells the JVM that this class implements clonable, so JVM treats that class as clonable
0
 
Gurvinder Pal SinghCommented:
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
dpearsonCommented:
The basic pattern for making an immutable object is to make all the member data final.  You initialize the values in the constructor:

public class MyImmutableClass() {
     private final int m_Value1 ;
     private final int m_Value2 ;

     public MyImmmutableClass(int val1, int val2) {
        m_Value1 = val1 ;
        m_Value2 = val2 ;
     }

    public int getValue1() { return m_Value1; }
    public int getValue2() { return m_Value2; }
}

This class is "immutable" because once you create an instance of the class, it will never change.  When you call "getValue1()" you will *always* get the same value.

Turns out this is very useful in multi-threaded programming because you can share an immutable object among different threads without any problems - no need for synchronize or similar logic, because the object will never change once constructed.

They're usually used for relatively simple classes.  Some Java classes (like Date) should have been immutable, but aren't.  That's because when Date was first written we didn't really understand the importance of immutable classes.

Doug
0
 
msk_apkCommented:
do not have set method.
have private fields.
have private constructor.
allow public get method.
0
 
Shura85Commented:
If you are implementing/extending an existing Java class, such as ArrayList<T>, have any method that would alter the list throw an UnsupportedOperationException.  Since this is a runtime exception, it does not have to be declared in the method header, which gets around the inheritance issue.
0
 
Shura85Commented:
Sorry if this comment is posted twice....got some bugs lately

@msk_apk
have private constructor

this does not affect the mutability of the list.  It's a step towards making it a singleton, but unless you define what will fill the list (and want it to be standard for all lists), its just a means of creation control.  The absence of setter methods + private fields make it immutable.  
0
 
msk_apkCommented:
what you said is right.
0
 
haneef_nbAuthor Commented:
Dear dpearson,

Thanks...

Ok, suppose my class has only setter methods, it doesn't have any final & private members, then can you please explain how can i prove that we can change the value further.  
0
 
Gurvinder Pal SinghCommented:
since you have the setter methods, you can invoke them to change the value
0
 
haneef_nbAuthor Commented:
sorry sorry, not setters , i am having only getter methods in my class, then it is not a immutable
0
 
Gurvinder Pal SinghCommented:
Yes, if there are no other methods also which can the object value, then the object becomes immutable.

Basically, even if the setter methods are there, all you have to make sure is that rather than making the changes in the current class (this variable), create a new object of the current class and return the same after setting the value
0
 
haneef_nbAuthor Commented:
Dear Experts,

see the below code snipet

public interface xxx
{
}

Please tell me how the jre knows it is a marker interface, and how a class which implements this interface , get special behavior

Please explain in brief..
0
 
haneef_nbAuthor Commented:
Thanks.......
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.