Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How to test if class/object is immutable

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to test if class/object is immutable

    I have make the immutable class as below, Now my question is how I can test if my this class/object is immutable

    package com.learning;

    import java.util.*;
    import java.util.Map.Entry;

    public final class ImmutableTest {

    private final int id;
    private final String name;
    private final HashMap<String, String> hm ;

    public int getId() {
    return id;
    }
    public String getName() {
    return name;
    }
    public HashMap<String, String> getHm() {
    return (HashMap<String, String> )hm.clone();
    }

    public ImmutableTest(HashMap<String, String> hm, int id, String name) {
    super();
    HashMap<String, String> temp = new HashMap<String, String>();
    this.id = id;
    this.name = name;
    Set<Entry<String, String>> s = hm.entrySet();
    Iterator<Entry<String, String>> itr = s.iterator();
    while(itr.hasNext()){
    Map.Entry<String, String> inentry = itr.next();
    temp.put(inentry.getKey(), inentry.getValue());
    }

    this.hm=temp;
    }

    }


    How I can Test If it is immutable class without looking ?


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How to test if class/object is immutable

    Can you define what you mean with an "immutable" class?
    Your class contains a hashmap which any outside user has access to. This means the contents of your hashmap can change over time. I would, personally, not say that this class is immutable, but if you define immutable different then thats your opinion.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to test if class/object is immutable

    immutable object (it cannot be changed once it's created). you mau always correct me if im wrong

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How to test if class/object is immutable

    Well, "changed" is a very broad definition.
    The name and id can not be changed, but the contents of the hasmap can. Do you count that as being changed?

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to test if class/object is immutable

    That exactly was my question how may I test it if the above class is immutable or not

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to test if class/object is immutable

    Please post your code correctly.

  7. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to test if class/object is immutable

    This is my code only, i need to know two things
    1) Whats wrong in my code
    2) how may i test my immutable class

    import java.util.*;
    import java.util.Map.Entry;

    public final class ImmutableTest {

    private final int id;
    private final String name;
    private final HashMap<String, String> hm ;

    public int getId() {
    return id;
    }
    public String getName() {
    return name;
    }
    public HashMap<String, String> getHm() {
    return (HashMap<String, String> )hm.clone();
    }

    public ImmutableTest(HashMap<String, String> hm, int id, String name) {
    super();
    HashMap<String, String> temp = new HashMap<String, String>();
    this.id = id;
    this.name = name;
    Set<Entry<String, String>> s = hm.entrySet();
    Iterator<Entry<String, String>> itr = s.iterator();
    while(itr.hasNext()){
    Map.Entry<String, String> inentry = itr.next();
    temp.put(inentry.getKey(), inentry.getValue());
    }

    this.hm=temp;
    }

    }

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to test if class/object is immutable

    Please read this topic to learn how to post code correctly and then correct the post above.

  9. #9
    Junior Member
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to test if class/object is immutable

    thanks for correcting me,I have read the article, so here is my questions

    [Query]
    Below I have written the immutable class, Please let me know
    1) Whats wrong in my code
    2) how may i test my immutable class
    [/Query]

     
    import java.util.*;
    import java.util.Map.Entry;
     
    public final class ImmutableTest {
     
    private final int id;
    private final String name;
    private final HashMap<String, String> hm ;
     
    public int getId() {
    return id;
    }
    public String getName() {
    return name;
    }
    public HashMap<String, String> getHm() {
    return (HashMap<String, String> )hm.clone();
    }
     
    public ImmutableTest(HashMap<String, String> hm, int id, String name) {
    super();
    HashMap<String, String> temp = new HashMap<String, String>();
    this.id = id;
    this.name = name;
    Set<Entry<String, String>> s = hm.entrySet();
    Iterator<Entry<String, String>> itr = s.iterator();
    while(itr.hasNext()){
    Map.Entry<String, String> inentry = itr.next();
    temp.put(inentry.getKey(), inentry.getValue());
    }
     
    this.hm=temp;
    }
     
    }
     
    [B][/B]

Similar Threads

  1. Replies: 2
    Last Post: April 8th, 2014, 12:05 PM
  2. how to test a class ??
    By ajw1993 in forum Java Theory & Questions
    Replies: 5
    Last Post: March 11th, 2013, 06:53 AM
  3. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  4. Convert File Object to class<?> object
    By CEO in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 27th, 2012, 06:55 PM
  5. To Make class immutable which has ref to other mutable objects
    By tcstcs in forum Object Oriented Programming
    Replies: 1
    Last Post: May 4th, 2012, 10:42 AM