aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Making own Generic class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Making own Generic class" Watch "Making own Generic class" New topic
Author

Making own Generic class

Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

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().


Astha - OCPJP 6 (90%)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4467
    
    8

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

Joined: Oct 15, 2011
Posts: 249

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

Joined: Apr 06, 2010
Posts: 4467
    
    8

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

Joined: Oct 15, 2011
Posts: 249

Got the point. Thanks Matthew.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 801
    
    1
It is just like

The compiler won't know what List<?> items will have at compile time.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 801
    
    1
One more note:
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 249

Very nice example Himai. Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making own Generic class