javamemory-managementjava-8jvmdefault-method

Where are the default methods of interface stored in memory?


I have gone through a number of posts, but all seem to answer that where are the static methods of an interface stored. However, an interface can have abstract, static and default methods. I know about static and abstract methods. But, I am not able to find anything related to default method storage in memory.

I might be wrong, but I am of the idea that default methods will be stored in static heap space just like instance methods are stored with class. But, on top of this as well, I am confused if default methods are also allocated to stack frames once they are called considering that the implementing class does not override the implementation of default method in interface and there is no diamond problem.

I have referred below links:

Where are methods stored in memory?

Where are static methods and static variables stored in Java?


Solution

  • There is nothing special for default methods as far as storage in the JVM memory is concerned. Like other class methods, they are part of the method area.

    I am confused if default methods are also allocated to stack frames once they are called considering that the implementing class does not override the implementation of default method in interface and there is no diamond problem.

    The stack frames are allocated when methods are invoked, again regardless of the kind of method (static, default, etc). Don't confuse their use during runtime invocation with where the method code (and other class metadata) is stored.