Just a bit of idle curiosity here.
Basically, if I have an object that only has a few primitive data members, it takes up a small amount of memory and doesn't take very long at all to create. However, what happens if I have a lot of methods associated with that object? Does object instantiation have to take those into account at all?
For example, let's say I have a Class with (insert absurdly large number here) number of distinct methods I can call. Does the JVM take any longer to make an instance of that class than if I had no methods?
No, Class
with methods is stored once in a separate memory location (namely PermGen
) and each object of a given class has only a single reference to its type (Class
).
Thus it doesn't matter how many methods your object has: two or two thousand - the object creation will take exactly the same amount of time.
BTW the same holds true for method invocation - there is no performance hit when calling methods of an object having plenty of them compared to object having only few.