Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Wednesday 12 October 2011

Creating Objects


                 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

Followers