Static Objects are not getting stored in HashMap.!

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Static Objects are not getting stored in HashMap.!



I am trying to create a Map with key as a String and Value as a static class. But when I am printing the data, it only stores the last key-value pair. Can someone help me with this.


import java.util.HashMap;
import java.util.Map;

public class MapImplementation

public static class Asset

public static String assetName;
public static String assetType;

private void setAssetName(String name)
Asset.assetName = name;


private void setAssetType(String type)
Asset.assetType = type;


private String getAssetName()
return assetName;


private String getAssetType()
return assetType;




public static void main(String args)

Map<String, Asset> map = new HashMap<>();
Asset asset1 = new Asset();
asset1.setAssetName("Vodafone");
asset1.setAssetType("STOCK");
map.put("Vodafone", asset1);

Asset asset2 = new Asset();
asset2.setAssetName("Google");
asset2.setAssetType("STOCK");
map.put("Google", asset2);

Asset asset3 = new Asset();
asset3.setAssetName("IBM");
asset3.setAssetType("BOND");
map.put("IBM", asset3);

for (String str : map.keySet())
Asset ast = map.get(str);
System.out.println(ast.getAssetName()+" "+ast.getAssetType());





The output I am getting is:


IBM BOND
IBM BOND
IBM BOND




1 Answer
1



Change:


public static String assetName;
public static String assetType;



to:


public String assetName;
public String assetType;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered