Creating objects means to allocate memory
space for all the instance variables of the objects. Step to create an object are,
1. Declare an object.
2. Define the object using new operator. The new operator
dynamically allocates memory for an object and returns a reference to it.
The general form is
1. Declare an object.
2. Define the object using new operator. The new operator
dynamically allocates memory for an object and returns a reference to it.
The general form is
classname obj;
obj=new classname();
|
Where
classname - defined
classname
obj - name of the object
new - keyword
Example
Student s1; //this declares an object s1 of type
student.
S1=new Student(); //this
allocates memory space for object s1.
The following figure shows
the representation of student object in the memory.
Statement Effect
Student s1; s1
S1=new Student();
The above
two steps can be combined into one step as,
Student
s1=new Student();
No comments:
Post a Comment