I am interested why this fragment of code produces a syntax error saying ImmutableMap cannot be resolved to a type:
ImmutableMap<String, String> advice = ImmutableMap<String, String>.builder()
.put(KEY1, VAL1)
.put(KEY2, VAL2)
.build();
While this code works as intended:
ImmutableMap<String, String> advice = ImmutableMap.<String, String>builder()
.put(KEY1, VAL1)
.put(KEY2, VAL2)
.build();
The period isn’t going where my mental model should and I was hoping someone could explain why the goes on the “method side” of the period. I’m using Guava’s ImmutableMap, but its not entirely relevant I don’t think. I think it has something to do with the generics but I’m not sure what and I’m not sure how to search to find a better answer since I don’t know what the concept would be called.
EDIT: For reference, ImmutableMap has this line for builder()
public static <K, V> Builder<K, V> builder() {
return new Builder<K, V>();
}
builder() is a static method which has generic types associated with it, not the class it is defined in. It will be something like
Note: the
<K, V>here doesn’t have anything to do with the generic types of the class. (Which doesn’t have to be generic)