• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Doubt

 
Akhil Maharaj
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I think I found this line some where in K&B Book . I am not sure however.

But can any of you provide more explanation to these lines .


When you write generic classes � you can use T with wild card �?� . When you write Generic methods � you can not use T with wild card ?
 
adam Lui
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look at page 604 it gives more detail,
for instanc, my guess

class myGen<T> {
T obj1;
void setObj (T whatever) {}
}


if we declare a class with a wildcard...

class myGen<?> {
? obj1;
}

it doesnt even compile.
 
adam Lui
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by adam lui:
look at page 604 it gives more detail,
for instanc, my guess

class myGen<T> {
T obj1;
void setObj (T whatever) {}
}


if we declare a class with a wildcard...

class myGen<?> {
? obj1;
}

it doesnt even compile.



if we try to add a method
public <? extends T> void myGen1(T something) {}

no, it doesnt compile neither!
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,

you cannot say "? extends T" because after the "extends" the compiler is wayting for a type, not a generic parameter.
Nitpicking: it would compile however if "T" was a real type of a class with name "T".

Yours,
Bu.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic