I had a lot of testcases running on the class MyClass, using it’s default constructor: MyClass().
Now the requirements of MyClass changed and the user can provide a HashMap to indicate some pairs . Now a MyClass needs to have at least one pair and throws exceptions if one of those is null.
I was hoping to create another default constructor to avoid having to rewrite all the test methods something like:
public MyClass() {
HashMap<KeyClass, ValueClass> hashMap = HashMap<KeyClass, ValueClass>();
hashMap.put(KeyClass.someValue, new ValueClass());
this(hashMap);
}
Now this doesn’t work, because i have to call the other constructor first, so i thought of writing some method
private static HashMap<KeyClass, ValueClass> getDefaultHashmap();
and using it to call the other constructor like this:
public MyClass() {
this(MyClass.getDefaultHashmap());
}
But this seemed to me as not really good style, so i was hoping you could tell me what the right way to do something like this is!
You could inline the HashMap creation:
But you’d have to ignore the serial-id warning to keep it “pretty”.