• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics Implementation in Java SE 5  RSS feed

 
Sriram Kannan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody,

I have been going through Joshua Bloch's "Effective Java". In the second chapter I found the following:

Using Static Factory methods to reduce the verbosity of creating parameterized instances.

Instead of,



the suggestion is a following Static Factory Method:



Therefore, the change would be



In the above example, can anyone tell me how this is done ?

Because until the assignment to "testMap", the Map object has no way of knowing what type of key-value instances are to be assigned to it.

This is different from a generic class.



Here, V can be considered a placeholder and we know it is replaced with "Integer".

But I am not seeing anything of that sort with the static factory method.

Can anybody explain how ?

Thanks in advance :-)
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's called type inference. It retrieves the V from the left-hand side. So assigning to a Test<String> will replace V with String, assigning to a Test<Integer> with Integer, etc.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!