Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - how much space does anonymous object take?

supposing the following scenario:

void thisIsCalledManyTimes(){
    // ....
    someObject.executeIfNecessary( new Runnable(){
         void run(){ //do sth here}
    });
}

how much space would the anonymous object take? I understand that each object anonymous obj would have a pointer to the same implementation of run in its method lookup table.

like image 472
Bober02 Avatar asked Dec 27 '22 15:12

Bober02


1 Answers

The source code of Runnable doesn't specify any fields, and so the anonymous class won't take any more space than an Object, with two differences. An inner class has an implicit reference to the outer class instance, so you would want to factor this in. It will also take copies of final variables referenced from the outer class.

like image 142
Brian Agnew Avatar answered Jan 08 '23 00:01

Brian Agnew