The Relationship Between Static Attributes and Heap Memory

A computer manages memory in such a way as to make room for different types of data so that as memory is requested by the computer, memory is always available. This memory comes in two forms: the heap and the stack. The heap is of particular importance to object-oriented programmers, particularly when designing and managing classes and objects in their programs. Even static class attributes and methods that are not tied to specific class instances need storage space, which in this case is the heap.

  1. Classes and Objects

    • Classes are blueprints for objects and define the structure of objects created from them, such as what data they contain and what methods they can use. When a programmer creates a class, the code from that class structures the class, tells the compiler what sort of variables and data it will be using and structures what kind of storage the class needs.

    Static Attributes of a Class

    • Classes, however, do not always have to become objects. Normally an object attribute is different for each object created. Certain attributes of classes can be declared as "static," meaning that they persist over multiple instances of objects created from that class. For example, an attribute "x" declared static in class "A" is accessible from all objects of type "A." Furthermore, static attributes of classes can be called from the class itself without an object.

    Objects and the Heap

    • The heap is a data structure that stores dynamically allocated data items created during program execution. More specifically, objects created from classes are allocated on the heap. In object-oriented programming languages, memory for objects is created during run time and placed on the heap. Once the object has no further use in the program, the programmer or the programming environment delete the object to free the memory.

    Static Attributes and the Heap

    • Static attributes are different: for normal attributes that will change from instance to instance, each object will have a separate variable instance of that attribute. So object "b" and object "c" with attribute "x" will have two separate instances of "x" stored in their memory space on the heap. Static attributes, however, persist. Therefore, special objects that describe classes themselves, rather than instances of objects, exist in the "permanent generation" of the heap. These special class objects will not delete from the heap but will always store static variables.

Related Searches:

References

Comments

Related Ads

Featured