• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using objects returned from a wildcard list

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic