-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.

1
  • 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. Commented Nov 23, 2019 at 10:59

1 Answer 1

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()); } } 
Sign up to request clarification or add additional context in comments.

6 Comments

No real reason to make var1 and var2 static fields, probably. Can just be local variables inside of main.
@Thilo: Yes that is true, I just edit his code to work, I will edit the answer
Now it is super-confusing. Why do you re-use the same variable instead of having map1 and map2?
@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.
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<String, String>();), my issue is resolved.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.