I have below code which overrides equals() and hashcode() methods.
public boolean equals(Object obj)
if (obj == null)
if (!(obj instanceof Name))
Name name = (Name) obj;
public int hashCode()
You can, but why would you? Option A: the compiler inlines that, so you end up with a reference to the field anyway. Option B: The compiler does not inline the call, i.e. you've introduced one extra method call.
There are also implications for legibility- if the
name field is directly accessible within the class, why not refer to it directly? I find this easier to read, but some people find it inconsistent.