Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8713865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:27:09+00:00 2026-06-13T05:27:09+00:00

Is it possible to declare a method while creating an object? I ran across

  • 0

Is it possible to declare a method while creating an object? I ran across the following lines of code in java:

public static void main(String[] args) {
        Comparator<String> comparator = new Comparator<String>() {
            public int compare (String s1, String s2) {
                return s1.compareToIgnoreCase(s2);
            }
        };
}

And it looks like that while creating the object comparator, the code is adding a method to implement the Comparator<T> interface. Is adding additional methods always possible when creating instances, or is it something in particular that’s related to a java interface?

Thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:27:10+00:00Added an answer on June 13, 2026 at 5:27 am

    This is not what you think it is.

    Whatever follows new Comparator<String>() is a anonymous inner class. It means that the anonymous class is implementor of Comparator class.

    You can have two oprions :

    1. AnyClass object = new AnyClass() { // anonymous inner class starts here.
    // In this case , inner class is a sub class of AnyClass.
    };// ends here
    2. AnyInterface anyInterface = new AnyInterface() { //anonymous inner class starts here.
    // In this case , inner class is implementer of AnyInterface.
    }; // ends here.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why would one declare a Java interface method as abstract? The following
Possible Duplicate: Cannot declare Public static final String s = new String(“123”) inside an
Is it possible in Java, to declare a method with a string instead of
Possible Duplicate: Why would one declare a Java interface method as abstract? I found
If I have the following method: public <U extends Number> void doSomething(List<U> l){ }
Is it possible to specify in DTD a method to declare new attributes in
In Java, is it possible to declare a field/variable whose type is multiple interfaces?
I've got the following method of an public abstract class Model { //impl }
I have the following method: public IEnumerable<Foo> GetFoo(int x, string y) { return from
Possible Duplicate: Run a code in given time interval I have a method that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.