-1

Main Class --

package test;
import java.util.Map;

public class Client {
    private static ArrayList<Class1> allInstances = new ArrayList<Class1>();
    private static Map <String, String> var1 = new HashMap<String, String>();

    public static void main(String[] args)
    {
        var1.put("key1","value1");
        Class1 instance1 = new Class1(var1);
        allInstances.add(instance1);

        var1.put("key2","value2");
        Class1 instance2 = new Class1(var1);
        allInstances.add(instance2);

        getInstances();
    }

    public static void getInstances() {
        for(Class1 c: allInstances) {
            System.out.println(c.getClassDetails());
    }
}

Class Class1 --

package test
import java.util.Map;

public class Class1 {
    private Map <String, String> classDetails;

    public Class1(Map <String, String> classDetails) {
        this.classDetails = classDetails;
    }

    public Map <String, String> getClassDetails(){
        return this.classDetails;
    }
}

Output--

{key2=value2}
{key2=value2}

As we can see from the output above, both instances variable returns the same updated value. Should'nt instance1 return {key1=value1}

Also, if this is the expected behavior, what can be done to tackle this issue.

Wamique
  • 59
  • 2
  • 5
  • The output doesn't match what your actual code prints (after fixing up your code - you've got an extra brace and you're missing imports). Fundamentally the problem is that both of your `Class1` instances use references to the same `HashMap` though. If you want them to be independent, they should have independent map objects. – Jon Skeet Nov 23 '19 at 10:59

1 Answers1

0

As it is appeared from your code, you referenced same HashMap to instacne1 and instance2 objects and in getClassDetails method the tostring method of same hashmap will invoked so the outputs is the same , use this code snippet :

import java.util.*;

public class Main {
    private static ArrayList<Class1> allInstances = new ArrayList<Class1>();

    public static void main(String[] args)
    {
        Map <String, String> var = new HashMap<String, String>();
        var.put("key1","value1");
        Class1 instance1 = new Class1(var);
        allInstances.add(instance1);

        var = new HashMap<String, String>();
        var.put("key2","value2");
        Class1 instance2 = new Class1(var);
        allInstances.add(instance2);

        getInstances();
    }

    public static void getInstances() {
        for(Class1 c: allInstances) 
            System.out.println(c.getClassDetails());
    }
}
  • No real reason to make `var1` and `var2` static fields, probably. Can just be local variables inside of `main`. – Thilo Nov 23 '19 at 11:09
  • @Thilo: Yes that is true, I just edit his code to work, I will edit the answer – Shayan Tabatabaee Nov 23 '19 at 11:13
  • Now it is super-confusing. Why do you re-use the same variable instead of having `map1` and `map2`? – Thilo Nov 24 '19 at 01:39
  • @Thilo : because the two instances of map is redundant, it seems that the maps is not the main points, and also if he want he can retrieve maps by its references. – Shayan Tabatabaee Nov 24 '19 at 04:28
  • Actually I am using same variable because I am not referencing this variable anywhere else, so why to use extra Space, I may be wrong but thats what I thought. Will making var1 static has to do something with standard guideline or just it is better to use? Could you please elaborate? @ShayanTabatabaee - Using your solution(var = new HashMap();), my issue is resolved. – Wamique Nov 24 '19 at 08:51
  • @Wamique: `var1` is one object that you passed reference of its to `Class1` objects, so actually there is only one `var1` in your code, I mean if you check `instance1.getClassDetails() == instance2.getClassDetails()` is true. static variables instantiate when class loads so basically the GC will not occurs on them until class unload from ClassLoader, It is better to use local variables that is belong to scope of method and will be clean from memory when the method ends. – Shayan Tabatabaee Nov 24 '19 at 09:56