File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making own Generic class

 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have a confusion in the topic Making own Generic class. Here is my code-

It shows following error in line 23 -
The method setItemToBuy(int, List<capture#1-of ?>) in the type Purchase<capture#1-of ?> is not applicable for the arguments (int, List<Animal>)

As per my understanding '?' shows that any type can be passed to the method argument. The why compiler is not allowing to pass List<Animal> as argument of method setItemToBuy().
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ? means that purchase is allowed to reference a Purchase object of any type. So it might be a Purchase<Animal>, Purchase<Lion>, Purchase<String>, Purchase<AnyOtherType> etc.

So when on line 23 you try to call it as if it's referencing a Purchase<Animal>, the compiler can't be certain that's going to be correct. It has no idea which type of Purchase is going to be referenced.

In this particular case the solution is easy: there's no reason to use a wildcard. Just declare the variable with the right type in the first place.
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matthew,
Thanks for the reply.
Matthew Brown wrote:So when on line 23 you try to call it as if it's referencing a Purchase<Animal>, the compiler can't be certain that's going to be correct.


Do you mean to say this code could be like this-

And in this case Animal can't be the argument. This is the reason the compiler doesn't allow to do such thing.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right. The compiler will only allow calls it knows are valid, and it only knows about the reference type of the variable, not what's actually going to be stored there.

Wildcards are useful when you need to write functions that are supposed to be able to act on lots of different generic types (like a lot of the methods in the Collections class - they don't often care what type of object is in the collection). In this case you're only asking it to work with Animal lists.
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got the point. Thanks Matthew.
 
Himai Minh
Ranch Hand
Posts: 1141
4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is just like

The compiler won't know what List<?> items will have at compile time.
 
Himai Minh
Ranch Hand
Posts: 1141
4
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more note:
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very nice example Himai. Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic