• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics Implementation in Java SE 5

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :-)
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic