Google+

Like Us On Facebook


Total Pageviews

66. 'class' is a template for an 'object'






It is important to remember that a class declaration only creates a template, it does not create an actual object.

Hence the below code does not create any objects

         class Box
        {
            double width;
            double height;
            double depth;
         }

To actually create a Box object, you have to use a statement like the following:

         Box mybox = new Box( );   //This statement will create a 'Box' class object called 'mybox'

Technically we can say that mybox object is an instance of Box class.

As mentioned earlier, each time you are creating an instance of a class, you are creating an object that contains its own copy of each instance variable defined by the class. In this example, mybox object has its own copy of each instance variable (i.e. width, height, depth) defined by the class Box.

To access the variables of an object we've to use the dot (.) operator. The following example will assign the value 100 to the width variable of mybox object.

          mybox.width = 100

Program to demonstrate that 'class' is a template for an 'object'                                                                           

1.  Launch Eclipse IDE
2.  Create a new Java Project and name it as 'Second Project' (How to create a project in Eclipse IDE is explained in Post#64 How to create projects in Eclipse IDE?
3.  Create a package [name it as 'second_package'] and add two class files [Name them as Box and BoxDemo] under it (How to add packages and class files to a project is explained in Post#65 Run Java Programs using Selenium IDE
4.  Finally it should look as shown below:



5. Edit the 'Box.java' and remove the code inside the 'Box' class. After removing the 'Box.java' should look like this:



6. Enter the following code into the 'Box.java' as shown below:

double width;
double height;
double depth;

After adding this code, the 'Box.java' file should be as shown below:



7. Save the 'Box.java' file and move to 'BoxDemo.java' Class file tab
8. Add the following code into the 'BoxDemo.java' Class file tab

------------------------------------------------------
  Box mybox = new Box();

double vol;

// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;

// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume of mybox is " + vol);
---------------------------------------------------------------

After adding this code, 'BoxDemo.java' Class file should look as shown below:



9. Save the 'BoxDemo.java' file
10. As we know out of the two files (i.e. Box.java and BoxDemo.java files). Only BoxDemo.java file has the main( ) to start the program and we've removed the main( ) method from Box.java file. So right click on the Java file which has the main( ) method i.e. BoxDemo.java file and select 'Run As' -> 'Java Application' as shown below:


11. Observe that the output is displayed as shown below:




Program to demonstrate that the class can be template for more than two objects

1. Launch Eclipse IDE
2.  Create a new Java Project and name it as 'Third Project'
3.  Create a package [name it as 'third_package'] and add two class files [Name them as Box and BoxDemo] under it.
4.  Box.java file should be changed to the following code and the file should be saved



5. BoxDemo.java file should be changed to the following code and the file should be saved



6. Right click on the class file having main( ) method i.e. BoxDemo.java file and select 'Run As' -> 'Java Application'

7. Observe that the output of this program is displayed as shown below:








Please comment below to feedback or ask questions.

How to export the Java project into your computer and how to import back into your Eclipse IDE will be explained in the next post.



2 comments:

MS said...

Hello Arun,

In second program of Third Project, point 5th: in screenshot the package name should be "third_package" instead of second_package.

Arun Motoori said...

@MS - Thanks. Updated :)

Blog Index