Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
One of the most significant ways in which object-oriented programs and their languages differ from traditional programming is in their use of memory. In older styles of programming, variables were declared in blocks and could be used throughout the block in which they were declared. (Sometimes the entire program was one block.) Variables were placed on the stack when control entered into a block, and when control left the block, the stack was cut back so that the variables were no longer accessible, and their memory was restored to the operating system.
With objects, there is a different pattern of use. It is not the mere declaration of a variable in a block that allocates memory; rather, it is the creation of the object that takes up memory. At the end of the object’s life cycle, the deletion of the object releases its memory. It is no longer a question of the scope of variables that determines their existence and memory usage but rather the programmer’s creation and destruction of the object. It is true that there are some cases in which an object is created and destroyed in a single block, but often an object is created in one place in a program and destroyed in another.