• 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

Newbie tries to use comparators... and fails.

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to understand how comparators work and to that end have created a simple program that will compare the integer variables in instances of a simple object. However, the comparator method fails to recognise the instance variables in the objects passed to it, giving a 'cannot find symbol' error at line 34 in Netbeans. I tried exactly the same program without the comparator implementation and it didn't give any errors, so I assume there is an aspect of the implementation/override aspect that I am goofing up. Go ahead and tell me what rookie error I am making this time Code below.

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your comparator class declaration should be:
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony's right. You might be interested in what the effect of your mistake is. When you write:
It's actually using ObjectCreate as a generic type variable, not as a concrete type. So your class is the equivalent of this:
Now you can why that error occurs. As far as the compiler is concerned, T can be anything. So it has no idea whether it will have a objectScore field or not.
 
Ian Taylor
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see!

I need to understand generics much better. I got my original syntax from the example on this page

http://tutorials.jenkov.com/java-collections/sorting.html

Corrected and it works fine now, thanks both of you!
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on 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