0

I happened about this declaration on the internet and found it really interesting. However I don't know what to search for to get more information about it. If you could tell me a bit about this or what to search for...

(the calling of methods on declaration)

 JPanel bluePanel = new JPanel(){{
        setBackground(Color.blue);
        setLocation(220, 10);
        setSize(50, 50);
 }};
Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420

1 Answers1

1

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.

Brian Agnew
  • 261,477
  • 36
  • 323
  • 432