Declaring a new object and calling some of its methods on creation

This question already has an answer here:

  • Efficiency of Java “Double Brace Initialization”? 15 answers
  • Is this a variation of an anonymous inner class? 3 answers

  • You're deriving an anonymous subclass of JPanel and then declaring a initialiser block for it.

    Here's the subclass:

    new JPanel(){};
    

    Note the braces. And the initaliser is declared within it:

    new JPanel() {
     { 
        // static initaliser
     }
    };
    

    The derivation of a subclass is simply to allow the initialiser block. This is called double-brace initialisation, and some worry about the abuse of creating an anonymous class simply for this purpose.

    See here for more info about the initialiser block.

    链接地址: http://www.djcxy.com/p/82394.html

    上一篇: 我如何在Java中加入两个列表?

    下一篇: 声明一个新对象并在创建时调用它的一些方法