0

I have seen in Javascript ,

var obj = {
}
function addMember(memberName,value) {
    obj[memberName] = value;
}

ie. objects can have members whose name is not known before at object/class declaration.So is there a similar way in Java ,where i can add new members to an object with unknown name?

I Hate Lazy
  • 47,415
  • 13
  • 86
  • 77
Jinu Joseph Daniel
  • 5,864
  • 15
  • 60
  • 90
  • 1
    http://stackoverflow.com/questions/11828160/adding-a-field-to-java-class Generally it means "no". – xeye Dec 03 '12 at 19:16

1 Answers1

5

You can do this with a Map.

Map<String, Object> map = new HashMap<>();

map.put(memberName, value);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130