• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Trying to understand <K, V> syntax in method name

 
Ranch Foreman
Posts: 2067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While creating a class, when I clicked on "create main method", eclipse created the below  for me:



instead of



I understand that generics is typically used to provide the type for a collection, while entering elements in it such that we dont face issues while retrieving from it and we dont require casting.  
What I dont understand is that what exactly does generic syntax mean with the method definition (as above)? What difference is this making in above case for example?

thanks
 
Marshal
Posts: 26129
77
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I create a class in Eclipse I right-click on a package and select New, then Class. I get a dialog box where I enter the class name and so on, and it has an area headed "Which method stubs would you like to create?" If I check the one labelled "public static void main(String[] args)" then it does what you would expect.

So I assume you followed some other process. Could you tell us about it?
 
Monica Shiralkar
Ranch Foreman
Posts: 2067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I remember now that how it had come .I had copy pasted some code from somewhere.

What does this generics syntax mean with method ?
 
Saloon Keeper
Posts: 12493
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same it does in all other places. It declares variables that hold a type. You can call the method and pass type arguments, usually for the purpose of getting a result that uses the same types.

Seeing as the main method has no return value and no parameters that use the type parameters, the declaration of the type parameters is useless. The original author likely made a mistake, and it shows you you should be careful when copying code without reviewing and understanding it first.
 
Monica Shiralkar
Ranch Foreman
Posts: 2067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I read on Generic methods and that we can pass 1 or more type parameters this way like <K> or <K, V>, or <K, V, M > etc.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic