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!

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

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
809 views