Online Software Educational Training - IT Tutorials - Online Education Training for Computer Software


Home

JAVA PROGRAMMING

A Simple Class & Objects

 Using Class and Objects

Here is a class called Box that defines three instance variables: width, height, and depth. Currently box doesn't contain method.

 

class Box {

double width;

double height;

double depth;

}

 

As stated, a class define a new type of data. In this case, the new data type is called Box. You will use this name to declare objects of type Box. 

 

To create a Box object, you will use a statement like the following:

 

Box mybox  new Box();            

 

After this statement executes, mybox will be an instance of Box.

 

If you want to assign any value to its attributes, you would use the following statement:

 

mybox.width=10;

 

 

Here is a sample code that uses the Box Class






Previous

Next