What do these braces do?

This is a snippet of code that was created by netbeans, and I am confused by what these brackets do after it calls AbstractListModel.

    li_reminderslist.setModel(new javax.swing.AbstractListModel() {
        String[] strings = { };
        public int getSize() { return strings.length; }
        public Object getElementAt(int i) { return strings[i]; }
    });

Sorry for the newbie. I tried to find a tutorial on this, but could not find it.

+4
source share
2 answers

It creates an anonymous inner class .

Basically, you create a new instance AbstractListModel, but redefine a few things. Since you are going to use it only in one place, it makes no sense to create a whole separate class declaration with its own name, hence the "anonymous" part.

, Java 8 - , / / .

+3

.

, Runnable:

new Runnable() {public void run() { }});

ActionListener

new ActionListener(){public void actionPerformed(ActionEvent e}{}};

.

+2

Source: https://habr.com/ru/post/1570736/


All Articles