aspose file tools*
The moose likes Java in General and the fly likes Using objects returned from a wildcard list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using objects returned from a wildcard list" Watch "Using objects returned from a wildcard list" New topic

Using objects returned from a wildcard list

Chad Schultz

Joined: Mar 25, 2007
Posts: 23
Hi all. My program accesses a service that returns a List<DeliveryCalendarDay<?>>, where DeliveryCalendarDay is a generic object to be parameterized with a specific calendar. My problem is that I have trouble using the actual objects from the list- I can't seem to find a way to tell the compiler that yes, I know what type of objects they are, so it's okay to let me access the methods. I've tried to read about wildcard parameterizations (it's very confusing to me!) and the only information I've found on this question (as far as I could understand what I read, anyway) is that it's a bad idea for methods to return such things. That doesn't help me, though- is there a way around this? Thanks!
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The compiler is not going to stop you from doing that. What you see when you compile is a warning, not an error.

You can still execute the program, but the compiler is warning you that it can't guarantee type safety.

If you don't want to see the warning, you can use the annotation @SuppressWarnings("unchecked") in JDK 1.5.0_06 or greater.
Chad Schultz

Joined: Mar 25, 2007
Posts: 23
Thanks, Keith. I figured out how to handle it, though. I used the object in the wildcarded form: so I took a DeliveryCalendarDay<?> object from the List<DeliveryCalendarDay<?>>. Then I was able to use all the features of the base class, which is fortunately all I needed anyway. I had to change every reference in my code from the specific subclass to this wildcard generic class, but it works, whee!
I agree. Here's the link:
subject: Using objects returned from a wildcard list
Similar Threads
'Generic' and int questions
generic confusion
Understanding Generics
Why is List<? super Number> a subtype of List<? super Integer> ?
Srping+Hibernate bad configurated causing huge memory consumption