aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes warning: [unchecked] unchecked method invocation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "warning: [unchecked] unchecked method invocation" Watch "warning: [unchecked] unchecked method invocation" New topic
Author

warning: [unchecked] unchecked method invocation

Mike Mitchell
Ranch Hand

Joined: May 28, 2008
Posts: 38
Hi guys. Apologies if this should be obvious. When I compile the following -Xlint I'm warned:

CompareMe.java:27: warning: [unchecked] unchecked method invocation: <T>sort(java.util.List<T> ;) in java.util.Collections is applied to (java.util.List<scratch.Item> ;)
Collections.sort(list);
^
1 warning

list is declared to hold only <Items>, and I assume Collections.sort is a generic method. What am I missing?

Thanks much.


[ June 18, 2008: Message edited by: Mike Mitchell ]

SCJP 5, SCWCD 5
Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
You need to make Item implement Comparable<Item>, not just Comparable (which is a raw type). That is:


Matt
Inquisition: open-source mock exam simulator for SCJP and SCWCD
Krishnamoorthy Vuyala Muralidharan
Ranch Hand

Joined: Sep 13, 2005
Posts: 52
public int compareTo(Object o) { Item i = (Item) o; if (this.number < i.number) { return -1; } else if (this.number > i.number) { return 1; } else { return 0; } }


Hi

compareTo() method's signature must be the same, as you are overriding it and not overloading. That means, you are implementing the method from the interface Comparable.

public int compareTo(Object o) {
Item i = (Item) o;
if (this.number < i.number) {
return -1;
} else if (this.number > i.number) {
return 1;
} else {
return 0;
} }

and not

public int compareTo(Item o) {
Item i = (Item) o;
if (this.number < i.number) {
return -1;
} else if (this.number > i.number) {
return 1;
} else {
return 0;
} }

Kind Regards
Kris
Mike Mitchell
Ranch Hand

Joined: May 28, 2008
Posts: 38
Thanks much Matt and Kris -- that got me straightened out.
 
Don't get me started about those stupid light bulbs.
 
subject: warning: [unchecked] unchecked method invocation