I was wondering if there exists a similar functionality in Java similar to C#\'s anonymous types:
var a = new {Count = 5, Message = \"A string.\"};
Maybe you mean sth like this:
Object o = new Object(){
int count = 5;
String message = "A string.";
};
@Commenters: of course this is a theoretical, very inconvenient example.
Probably OP may use Map
:
Map a = new HashMap();
a.put("Count", 5);
a.put("Message", "A string.");
int count = (Integer)a.get("Count"); //better use Integer instead of int to avoid NPE
String message = (String)a.get("Message");