This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic compilation issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic compilation issue" Watch "Generic compilation issue" New topic
Author

Generic compilation issue

chirag visavadia
Greenhorn

Joined: Dec 21, 2009
Posts: 6



Can anyone please help me to understand this code? As per my understanding below code should take Object() as argument but it gives compile error. Thanks

GenericTest.java:5: cannot find symbol
symbol : method add(java.lang.Integer)
location: interface java.util.NavigableSet<capture#964 of ?>
set.add(new Integer(2));
^
1 error


OCPJP6
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 724
The generic set with <?> won't let you add anything except null as a rule of the compiler to make your collection type safe.

During compilation time, the set means it refers to a collection that contains a type of object as specified on the right hand side.
During compilation time, the compiler does not know what set will contain. It can be Object, Animal, Cat, Dog....
For example, you may want to add this to the class in the future:

Again during compilation time, set.add( ...) will not compile because the compiler does not know what set will contain. The compiler won't let you put a wrong type of object to set.
chirag visavadia
Greenhorn

Joined: Dec 21, 2009
Posts: 6

Thank you now I understand...
 
jQuery in Action, 2nd edition
 
subject: Generic compilation issue
 
Similar Threads
unexpected compilation error with Generics
generics
Question related Typecasting in Generics
Generic not compiling
Generic Colletion <? super String>