3 ms·
The compiler generates the anonymous class. To the runtime, isn't it just a class with a name given to it by the compiler, not a developer (question mark...my k
by scotth 17y ago
The compiler generates the anonymous class. To the runtime, isn't it just a class with a name given to it by the compiler, not a developer (question mark...my key broke today)
It's true that it uses up permgen though, but if it only adds an initializer we're not talking about much space.
Edit:
I did an experiment. This is the code:
new ArrayList<Object>() {
{}
};
new ArrayList<Object>();
Here is the bytecode:
0 new Main$1 [16]
3 invokespecial Main$1() [18]
6 new java.util.ArrayList [19]
9 invokespecial java.util.ArrayList() [21]
And, the class:
class Main$1 extends java.util.ArrayList {
// Method descriptor #6 ()V
// Stack: 1, Locals: 1
Main$1();
0 aload_0 [this]
1 invokespecial java.util.ArrayList() [8]
4 return
Line numbers:
[pc: 0, line: 10]
[pc: 4, line: 1]
Local variable table:
[pc: 0, pc: 5] local: this index: 0 type: new Main(){}
Just a regular old subclass.