How are Anonymous inner classes used in Java?

后端 未结 18 1331
孤独总比滥情好
孤独总比滥情好 2020-11-21 05:19

What is the use of anonymous classes in Java? Can we say that usage of anonymous class is one of the advantages of Java?

相关标签:
18条回答
  • 2020-11-21 05:46

    You can use anonymous class this way

    TreeSet treeSetObj = new TreeSet(new Comparator()
    {
        public int compare(String i1,String i2)
        {
            return i2.compareTo(i1);
        }
    });
    
    0 讨论(0)
  • 2020-11-21 05:47

    Anonymous inner class can be beneficial while giving different implementations for different objects. But should be used very sparingly as it creates problem for program readability.

    0 讨论(0)
  • 2020-11-21 05:48

    You use it in situations where you need to create a class for a specific purpose inside another function, e.g., as a listener, as a runnable (to spawn a thread), etc.

    The idea is that you call them from inside the code of a function so you never refer to them elsewhere, so you don't need to name them. The compiler just enumerates them.

    They are essentially syntactic sugar, and should generally be moved elsewhere as they grow bigger.

    I'm not sure if it is one of the advantages of Java, though if you do use them (and we all frequently use them, unfortunately), then you could argue that they are one.

    0 讨论(0)
  • 2020-11-21 05:50

    They're commonly used as a verbose form of callback.

    I suppose you could say they're an advantage compared to not having them, and having to create a named class every time, but similar concepts are implemented much better in other languages (as closures or blocks)

    Here's a swing example

    myButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            // do stuff here...
        }
    });
    

    Although it's still messily verbose, it's a lot better than forcing you to define a named class for every throw away listener like this (although depending on the situation and reuse, that may still be the better approach)

    0 讨论(0)
  • 2020-11-21 05:54

    I use them sometimes as a syntax hack for Map instantiation:

    Map map = new HashMap() {{
       put("key", "value");
    }};
    

    vs

    Map map = new HashMap();
    map.put("key", "value");
    

    It saves some redundancy when doing a lot of put statements. However, I have also run into problems doing this when the outer class needs to be serialized via remoting.

    0 讨论(0)
  • 2020-11-21 05:55

    Anonymous inner class is used in following scenario:

    1.)For Overriding(Sub classing) ,When class definition is not usable except current case:

    class A{
       public void methodA() {
          System.out.println("methodA");
        }
    }
    class B{
        A a = new A() {
         public void methodA() {
            System.out.println("anonymous methodA");
         }
       };
    }
    

    2.)For implementing an interface,When implemention of interface is required only for current case:

    interface interfaceA{
       public void methodA();
    }
    class B{
       interfaceA a = new interfaceA() {
         public void methodA() {
            System.out.println("anonymous methodA implementer");
         }
       };
    }
    

    3.)Argument Defined Anonymous inner class:

     interface Foo {
       void methodFoo();
     }
     class B{
      void do(Foo f) { }
    }
    
    class A{
       void methodA() {
         B b = new B();
         b.do(new Foo() {
           public void methodFoo() {
             System.out.println("methodFoo");
           } 
         });
       } 
     } 
    
    0 讨论(0)
提交回复
热议问题