Java – what is this language feature called?

JPanel panel = new JPanel() {
JPanel panel = new JPanel() {
        public void setBackground(Color c) {
           Logger.global.info("setBackground: c=" + c);
           super.setBackground(c);
        }
    };

All I know is that I can do JPanel panel = new jpanel();

Why would anyone do that? What's its name?

Solution

It is called an anonymous class

This code basically creates a subclass of JPanel "dynamic" without giving it a name (so called anonymous class) and instantiating it

Related questions and links:

> Mindprod. com,Java Glossary: Anonymous classes > Is usage of anonymous classes in Java considered bad style or good?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>