Does an instance method get loaded into memory per object or per class?
If I have an object O
with a gigantic method f()
, and I load 10000 examples of O
into memory. Are 10000 examples of f()
loaded into memory as well? If so, does that mean that I开发者_开发问答 would save memory by making this function static if possible?
Instance Methods are loaded in to Method Area in JVM. it is loaded once , but there will be many stack for every call u make to f() , to keep track of there own local variable values.
No. There is only one instance of the method loaded.
Instance method is just a template and is defined in a class (not in every instance). You wouldn't save memory by making it static.
No. Methods are not part of instances; they're part of classes. There would be no point in repeating the code for each instance (because it would never vary) so the implementation is, quite simply, smarter than that.
精彩评论