1

First of all, I have to admit that I do not search my problem on the web because I don't know which keywords match with my issue. I have just a couple hour experience with java and I have came across with the following syntax:

public class Simulation extends JFrame {

    // some fields...

    public Simulation() {
    ActionListener listener1 = new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    InputStream pauseStream;
                    try {
                        pauseStream = new FileInputStream("/pause.wav");
                        PM = new AudioStream(pauseStream);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }



                    setPause(!isPause());
                    if(isPause()) {
                        button1.setText("Play");
                        MGP.stop(BGM);
                        MGP.start(PM);
                    } else {
                        button1.setText("Pause");
                        MGP.start(PM);
                        MGP.start(BGM);
                    }


                }
            };
    }
       // other methods
}

Unfortunately, I did not understand meaning of the following line (I mean whether its function, class or something like that) ActionListener listener1=new ActionListener() { because, as I remember there is no correspondence in C or C++ .

If I'm not wrong, by ActionListener listener1=new ActionListener() should create an ActionListener instance but what is the code in following curly brackets ?

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
underRated
  • 39
  • 3
  • 8
    http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html – Konstantin V. Salikhov Feb 17 '15 at 19:02
  • @KonstantinV.Salikhov thanks a lot, as I said before I could not search it since I did not know what is the name of this fact but now I learnt it – underRated Feb 17 '15 at 19:03
  • 3
    Start with a beginners book instead. – Johan Feb 17 '15 at 19:03
  • 5
    You're creating an **anonymous inner class** object with that code. Since ActionListener is an *interface* and not a concrete class, you cannot simply create an instance via `new ActionListener()` but rather must create an instance from a concrete child class that implements ActionListener, and this is one short-hand way to do this. It's called *"anonymous"* because the class created has no name and cannot be referred to (easily) (while the object created this way *can* be referred to easily since you've assigned it to a variable). – Hovercraft Full Of Eels Feb 17 '15 at 19:03
  • @Radiodef as I said in the question, I did not know how to search my problem on web or stackoverflow so I think it is not a dublicate – underRated Feb 17 '15 at 19:21
  • Or possibly [Curly braces in “new” expression? (e.g. “new MyClass() { … }”)](http://stackoverflow.com/questions/10468806/curly-braces-in-new-expression-e-g-new-myclass). @underRated Just because you didn't search doesn't mean answers don't already exist. For example try Googling *"java curly brackets new class site:stackoverflow.com"*. A whole lot of Q&As come up. You didn't need to know what it was called. – Radiodef Feb 17 '15 at 19:29

1 Answers1

1

The code following curly brackets is syntax for an anonymous class. This is similar to a lambda expression you may have seen in C++11, except that it defines an entire class versus just one function (so an anonymous class instead of an anonymous function).

NESPowerGlove
  • 5,496
  • 17
  • 28