This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

generics - methods can’t (re)define type parameter section

 
Ranch Hand
Posts: 59
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Manning MEAP OCP7, it says:

These methods can’t (re)define type parameter section. The following won’t compile:



which is on my understanding means that once you declare a type parameter of X in the class declaration, declaring a type parameter of X in a method level will result to compile error. But trying those out results to compiling successfully.




Am I assuming wrong in my understanding that I have mentioned or am I missing something?


Thanks!
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yin,

Can you give a bit more context? Frankly, I don't understand what original code in the book is trying to achieve.

Your code works because:
1) In MyInterface, you are defining a method print - which returns X - and X is generic type (hence <X>). Try <X> print(X t) and it won't work (but again, I'm unable to understand why somebody would do that).
2) In MyClass, you are mentioning that X is generic (i.e. <X>) and method print returns void (thus, <X> is actually redundant).
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic