tags:

views:

315

answers:

2

I learned that class fields are stored in the heap, but where are methods stored? In the heap or somewhere else? are they inline?

+1  A: 

Class methods are stored together with all code in a dedicated segment of program memory meant specifically for storing code. Each method's code is stored once.

sharptooth
+7  A: 

Methods are stored somewhere else in the memory. Notice that methods are per-class, not per-instance. So typically, the number of methods doesn't change over the run-time of a program (there are exceptions). In traditional models, the place where the methods live is called the "code segment". In .net, it's more difficult: the methods originally live in the assembly, and get mapped into the process memory. There, the just-in-time compiler creates a second copy of some methods in native code; this copy gets executed. The JIT code may get created and deleted several times during the runtime, so it is practical to view it also as living "in Heap".

Martin v. Löwis
+1 I loved the explaination...
Prashant
I didn't say "pre-class", but "**per**-class". Methods are per-class: this means that every method exists only once, for the class. Different instances have still the same methods. Attributes/properties are per-instance: an attribute exists for every instance, i.e. different instances have different values for an attribute.
Martin v. Löwis