Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
In C# or Java we find that the objects are stored on heap and their reference vars are stored on stack. But at run time where is the class definition stored to be used as a template for creating objects?
Posted

It is not stored anywhere. The class definition is used by the compiler to generate the executable code in the final program. If you mean the items that can be found via reflection then that information is part of the generated code in fixed memory.
 
Share this answer
 
The class itself is stored in the code of your assembly and its metadata. .NET is one of the platform where all the information on all types is stored in the corresponding assemblies. .NET metadata is one of the central concepts of .NET; it makes reflection possible. You really need to learn it:
Type Class (System)[^],
Reflection (computer programming) - Wikipedia, the free encyclopedia[^],
Metadata and Self-Describing Components[^],
Reflection in the .NET Framework[^].

The central class is the class System.Type (referenced above). The instance of this class holds all one may need to use the type, including the references to the IL code of each method. All this can be retrieved using reflection.

The term "reference" is related to the instance of the class, not class. Nevertheless, the instance always keep the reference to its type, it can be retrieved via the method System.Object.GetType():
Object.GetType Method (System)[^].

—SA
 
Share this answer
 
In Java, the class itself can only be stored on the heap.
It has to be there because the static fields should be accessible during the lifetime of the VM.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900