Memory allocation when constructing a new object

When the computer constructs a variable, it allocates memory to hold its value and gives it a name. For example, when the statement:

     Dim intAge As Integer = 20

executes, it allocates four bytes of memory (enough to hold an integer), gives it the name intAge, and assigns an initial value of 20.

When a point is constructed, memory is allocated to hold the values of its properties. For example, when the statement:

     Dim myPoint As Point = New Point(75, 25)

executes, it allocates memory to hold the values of the properties of a Point.

It also creates an object variable called myPoint. That variable does not contain a value, but a pointer. The pointer is the address of the block of memory that contains the property values of the object (myPoint).

There is also a pointer to the location in memory of the Point class, where there is a subprogram for each method of the class. For example, the instructions for executing the toString method would be found here.

The object definition pulls together both the object data (its property values) and the actions it can take (its methods). Packaging data and functionality together objects let us create modular programs. This helps programmers cope with the complexity of large programs.


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.