wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wildcard Insantiation warped???? (Posted in Java Advance also) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic

Wildcard Insantiation warped???? (Posted in Java Advance also)

Sachin Ahuja

Joined: Aug 12, 2005
Posts: 13
There seems to be a major caveat in Tiger regarding wildcard instantiation (or perhaps I haven't rationalized it well).

Here's the deal ..
Lets say we have a class heirarchy as follows:
SimpleVO implements IValueObject which extends from IVO
Now, lets define a generic class like this :
ValueObjectContainer<T extends Integer>
Or even -
ValueObjectContainer<T extends IVO>

If we now have to instantiate this class using a wildcard declaration like this ..
ValueObjectContainer<? super/extends Blah> simpleContainer;

the java compile time system doesn't take the trouble of figuring out if your generic declaration is in sync with the class's type declaration.

Which is to say that if your BLAH is not in the type heirarchy of the bound specified in the class definition ... the compiler essentially doesn't give a damn.

Now, If you have instantiated your class using a lower bounded wildcard <? super Blah>, you can call methods on the instance as long as you are passing subtypes of the bound.

BUT this will throw a NoSuchMethodError in the runtime.
The compile time is only gonna check your declaration and instantiation and NOT bother about the class at all.

Can anyone throw any light on this???

BTW, I have started creating my Tiger notes, for those of you who are interested you can find them on www.sachinahuja.com
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Closing duplicate post. Please on no account duplicate posts.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link: http://aspose.com/file-tools
subject: Wildcard Insantiation warped???? (Posted in Java Advance also)
It's not a secret anymore!