Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Comparator object instantiation

840446Mar 29 2011 — edited Mar 31 2011
This is probably a dumb question so bear with me. Why doesn't this work?
static final Comparator<Product> SENIORITY_ORDER = new Comparator<Product>();
Error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	at OrderProcessor.main(OrderProcessor.java:178)
This works:
static final Comparator<Product> SENIORITY_ORDER = new Comparator<Product>(){
        public int compare(Product e1, Product e2) {
            return 1;
        }
    };
I Just had it return 1 just for testing. The compare method has nothing to do with my question. I just don't understand why the top code doesn't work and the bottom code does work.?

Referring to the bottom code, is that what an anonymous inner class is?
If yes, then why do I have to use an anon class to instantiate the Comparator object?

Thanks
This post has been answered by 796440 on Mar 29 2011
Jump to Answer

Comments

abillconsl
Because Comparator is an interface, not a class. What you did the second time around was flesh it out.

Edited by: abillconsl on Mar 29, 2011 2:35 PM

IOW: compare must be implemented.
796440
abillconsl wrote:
Because Comparator is an interface, not a class. What you did the second time around was flesh it out.
More precisely, it's because Comparator is abstract (as are all interfaces), and the anonymous inner class is concrete, with implementations for all of Comparator's abstract methods. The exact same behavior occurs with abstract classes, and for the exact same reason. :-)
840446
abillconsl wrote:
Because Comparator is an interface, not a class. What you did the second time around was flesh it out.
Oh ok. That makes sense. Just curious. The second block that I wrote is an anon class???
796440
Answer
SquareBox wrote:
The second block that I wrote is an anon class???
Yes.
Marked as Answer by 840446 · Sep 27 2020
abillconsl
Not exactly anon with the named variable SENIORITY_ORDER that you can use as a handle to call compare on it though.
EJP
The 'second block that he wrote' is indeed an anonymous class. Please don't just create confusion.
796440
abillconsl wrote:
Not exactly anon with the named variable SENIORITY_ORDER that you can use as a handle to call compare on it though.
Yeah, it's anonymous. Having a variable refer to it doesn't change that. It's the fact that the class itself is defined inline and has no name that we can use to refer to it that makes it anonymous.
abillconsl
jverd wrote:
abillconsl wrote:
Not exactly anon with the named variable SENIORITY_ORDER that you can use as a handle to call compare on it though.
Yeah, it's anonymous. Having a variable refer to it doesn't change that. It's the fact that the class itself is defined inline and has no name that we can use to refer to it that makes it anonymous.
Of course you're both right, the class has no name - that's the important point. I didn't mean to confuse anyone, and I apologize.

OP, probably, usual practice is that anon classes are declared without a reference variable attached:
new Comparator<Product>() { ... }
As opposed to:
Comparator<Product> SENIORITY_ORDER = new Comparator<Product>() { ... }
Looking at the compiled .class names will usually show though, that, as opposed to a true local class coded more like this:
class MyComparator<String> implements Comparator<String> {
- that has a name:
MyClass$MyComparator.class
The anon class will likely be named as:
MyClass$1.class
Thus the lack of a class name.
796440
abillconsl wrote:
jverd wrote:
abillconsl wrote:
Not exactly anon with the named variable SENIORITY_ORDER that you can use as a handle to call compare on it though.
Yeah, it's anonymous. Having a variable refer to it doesn't change that. It's the fact that the class itself is defined inline and has no name that we can use to refer to it that makes it anonymous.
Of course you're both right, the class has no name - that's the important point. I didn't mean to confuse anyone, and I apologize.

OP, probably, usual practice is that anon classes are declared without a reference variable attached:
I disagree. I don't have solid numbers, but I do know that in my own case, a pretty significant number of my anonymous classes to get a reference variable attached by me at the point I declare them. And even if I don't assign the reference to a variable, whatever I hand that object off to will.
abillconsl
jverd wrote:
abillconsl wrote:
jverd wrote:
abillconsl wrote:
Not exactly anon with the named variable SENIORITY_ORDER that you can use as a handle to call compare on it though.
Yeah, it's anonymous. Having a variable refer to it doesn't change that. It's the fact that the class itself is defined inline and has no name that we can use to refer to it that makes it anonymous.
Of course you're both right, the class has no name - that's the important point. I didn't mean to confuse anyone, and I apologize.

OP, probably, usual practice is that anon classes are declared without a reference variable attached:
I disagree. I don't have solid numbers, but I do know that in my own case, a pretty significant number of my anonymous classes to get a reference variable attached by me at the point I declare them. And even if I don't assign the reference to a variable, whatever I hand that object off to will.
The way I phrased my reply allows anyone to freely disagree, without hard facts. What I meant was simply that in most of the cases a beginner will see - via tutorials for example - will likely be the w/o the ref variable. I say likely, only because that is my own experience. Again, anyone is obviously free to disagree.
EJP
OP, probably, usual practice is that anon classes are declared without a reference variable attached
Except in the other cases. I fail to see the relevance of this remark. His question is about 'comparator object instantation', and this is a perfectly legitimate way to do it, if the Comparator is stateless.
The way I phrased my reply
The way you phrased your reply was that your statement was 'probably' true, and it was indeed 'without hard facts' of your own. Dubious and irrelevant.
YoungWinston
SquareBox wrote:
If yes, then why do I have to use an anon class to instantiate the Comparator object?
Simple answer is: you don't.
This works too:
static final class SeniorityOrder
       implements Comparator<Product>{
   public SeniorityOrder() {}
   public int compare(Product e1, Product e2) {
      return 1;
   }
};
And then whenever you need one, use 'new SeniorityOrder()'. I hope that isn't your actual comparison method though :-).

Winston
1 - 12
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Apr 28 2011
Added on Mar 29 2011
12 comments
802 views