• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Regarding class implementing Comparator interface

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I would like to know that is it necessary to have a static class for implementing a Comparator interface? and if it is then why?

Thanks in advance,
Saumya
 
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that isn't necessary. You could try writing a top-level class to implement Comparator yourself, if you liked.
 
Saumya Srivastava
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, thank you for the information.
But as you said when I tried to run following class. It runs successfully when class is declared static. However, if I remove static from the class ReSortComparator that is implementing Comparator interface then it throws Exception when I am creating instance of the class at line no. 26.

 
Paul Clapham
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see. But if that was your question, why didn't you ask it in the first place?

As for your exception, it's hard to say anything about it when we can't see it. Could you post the stack trace?
 
Saumya Srivastava
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry it's a compilation error at the same line no. 26. It says that ::

No enclosing instance of type SearchObjArray is accessible. Must qualify the allocation with an enclosing instance of type SearchObjArray (e.g. x.new A() where x is an instance of SearchObjArray).

I didn't get it.

Thanks,
Saumya
 
Saumya Srivastava
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

My apologies. I messed up with the braces in hurry and put the class inside the outer class. It was the error because of that reason.
So you are correct that static is not required to define with the class implementing the Comparator Interface.

Thanks,
Saumya
 
reply
    Bookmark Topic Watch Topic
  • New Topic