Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
    Bookmark Topic Watch Topic
  • New Topic

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

Sachin Ahuja
Posts: 13
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Report post to moderator
Closing duplicate post. Please on no account duplicate posts.
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!