This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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
Author

Using objects returned from a wildcard list

Chad Schultz
Greenhorn

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
Greenhorn

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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using objects returned from a wildcard list
 
Similar Threads
generic confusion
'Generic' and int questions
Srping+Hibernate bad configurated causing huge memory consumption
Understanding Generics
Why is List<? super Number> a subtype of List<? super Integer> ?