For how long the generics in Java was that smart ?

Ok, in the middle of the night, when I was writing code for one of my projects, I needed to use a generic method; which allows you to specify a generic type for arguments when calling that function. Which is a something I'm already familiar with. I had a function like this:

private void addProperty(String name, T defaultValue) {
....
}


When I wanted to call this from another function, I wrote something like this:

this.addProperty("age", 12);


Actually I expected to see an error or at least a warning. But I was wrong. When I hovered over this call in Eclipse, an information popup appeared:

void .ModelPropertyMap.addProperty(String name, Integer defaultValue))


Looks like Java compiler is able to recognize the type of argument you pass to a generic argument, and process it like I called with Integer generic argument:

this.addProperty("age", 12);


I think it is Java compiler that gives us such a flexibility, but I'm using Eclipse and maybe it's one of the IDE's features; I'm not sure, so I would appreciate if anyone could give me an insight about that.

Comments

  1. Thank you for taking the time to publish this information very useful! Brand

    ReplyDelete

Post a Comment

Popular Posts