On Feb 2, 11:37 am, Alexey Zinger <[email protected]> wrote:
> public Map<String, Integer> getAges()
> {
> return new HashMap<String, Integer>()
> {{
> put("kitty", 7);
> put("puppy", 2);
> put("fishy", 3);
> }}
>
> }
>
> What it really is is an anonymous inner class that extends HashMap and has an
> instance initializer block (as opposed to static initializer block). Let's
> rewrite above in a clearer fashion:
This is also a bad idea for any class with non-default equals()
and hashCode() methods. The typical equals() method looks
like
public boolean equals(Object other) {
if(other == null ||
getClass() != other.getClass()) {
return false;
}
// stuff
}
If one of this and other are examples of an instance-initialized
inner class, this will return false. That's okay, unless that
instance is put into a HashSet, or becomes a HashMap key.
Eric
--
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.