This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Murach's Java Programming and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

generics error  RSS feed

 
karthik swamy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i am getting this error after compiling :



so please suggest me where i am wrong.


Thanks,
karthik swamy.



 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your *public T getData() * expect the generic return type T . but you have returned a string!

so change the method return type as String[preferable approach] else cast the string to T and return as in *return (T)s*;

<edit>added clarity</edit>
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and please use code tag when you post code!
 
karthik swamy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:your *public T getData() * expect the generic return type T . but you have returned a string!

so change the method return type as String[preferable approach] else cast the string to T and return as in *return (T)s*;

<edit>added clarity</edit>


thanks its working,but can it wil work for integer type
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karthik swamy wrote:but can it wil work for integer type

actually *return (T)s* this lead you in confusion. that is why I said first approach is preferable. Yes , it will work for an Integer type also
 
karthik swamy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
karthik swamy wrote:but can it wil work for integer type

actually *return (T)s* this lead you in confusion. that is why I said first approach is preferable. Yes , it will work for an Integer type also



but generics is used for auto typecast then why we are typecasting like return (t)s
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karthik swamy wrote:but generics is used for auto typecast then why we are typecasting like return (t)s

your T getData implementation force us to do that! you should reimplement[even signature of method] the method!
 
Destroy anything that stands in your way. Except this tiny ad:
Thoughts on deprecation in Java
https://coderanch.com/t/683016/java/Deprecation-Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!