Google+

177. Defining and Creating Objects







As we have already gone through the concept of Classes and objects in my earlier posts, lets find out how clear we are about these concepts using the below questions and explanations.

What is a Class ?

Class contains instance variables and methods. Data resides in the instance variables where as the programming code resides in the methods as shown below -



Can we define a variable using the Created Class ?

Yes, like int defines an integer variable, char defines a character variable, we can use a created Class to define variables of its type.

Example -

1. Lets Create a Class named 'Car' as shown below -

class Car
{
       int a,b,c;

       public void sum( )
       {
            int sum = a+b+c;
            System.out.println("Sum is "+sum);
        }

}

2. Lets create another Class 'One' and lets define a variable of 'Car' Class type -

class One
{
      public static void main(String args[])
      {

           Car benz;   //Defining a variable benz of  Car Class data type

       }

}

So similar to the primitive data type int, double, char etc. we define variables of Class type.

Later we can use these variables to create objects.

How to create objects using the Class type defined variables ?

First we need to understand that Class declaration as shown below only creates a template, but don't create any object.

Car benz;    //This Car Class type declaration of variable benz will only create template but don't create any object

Thus the above statement wont cause any object to come into the existence, it will just define the variable as the specified Class type i.e. Car Class type.

So, what should we do to create an object ?

Instead of just defining a variable of any Class type, we have to use the below statement to define a variable of the given Class type and create object at the same time.

Car benz = new Car( );

Here in the above statement we have defined the benz variable as Car Class type and also we have allocated memory using new keyword to create the object benz of Car Class.

Does created object contain its own copy of instance variables and methods of the defined Class type ?

Yes, after creating an object benz, the object will have a copy of instance variables and methods of a Car Class as shown below -



What is the use of new keyword in the object defining and creating statement ?

Obtaining objects of a Class is a two step process. First, you must declare a variable of Class type. Second, you must acquire actual physical copy of the object and assign to the defined variable. You can do this using the new operator. The new operator allocates memory to the object and returns a reference to the defined variable.

So using new we allocate memory to the physical object of the Class and assign the created object reference to the variable defined with the Class Type. Hence we use new operator in the object creation statement as shown below -

Car benz = new Car( );

Lets break the above statement into two and understand their uses -

Car benz; //Declares reference to object

benz = new Car( );  //Allocates a Car Class object


So in our example,  benz is a variable of Class Car type and refers to the object allocated by the new operator.


Constructor in the object creating statement 

Car( ) in the above object creation statement is the constructor of Car Class. Please go though the posts on Constructors in my blog to understand the constructor concept in depth.




Please comment below to feedback or ask questions.

Assigning 'Object Reference' variables will be explained in the next post.






No comments: