Java class definition

Introduction

A class defines a new type of data.

A class is declared by use of the class keyword.

A simplified general form of a class definition is shown here:

class classname  {  
   type instance-variable1;  //from   ww w .j  av a2s.  com
   type instance-variable2;  
   // ...  
   type instance-variableN;  
   
   type methodname1 (parameter-list ) {  
     // body of method  
   }  
   type methodname2 (parameter-list ) {  
     // body of method  
   }  
   // ...  
   type methodnameN (parameter-list ) {  
       // body of method  
   }  
} 

The data defined within a class are called instance variables.

The methods and variables defined within a class are called members of the class.

The methods act on a class' data.

Variables within a class are called instance variables.

Each instance of the class contains its own copy of the variables.

The data for one object is separate from the data for another.

A Demo Class

Here is a class called LegoBlock that defines three instance variables: width, height, and depth.

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

A class declaration only creates a template.

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

LegoBlock myLegoBlock = new LegoBlock(); // create a LegoBlock object called myLegoBlock 

myLegoBlock is an instance of LegoBlock.

To assign the width variable of myLegoBlock the value 100, use the following statement:

myLegoBlock.width = 100; 

Here is a complete program that uses the LegoBlock class:

/* A program that uses the LegoBlock class.  
*/  /*from w  w  w  . j av a  2s.  c  om*/
class LegoBlock {  
  double width;  
  double height;  
  double depth;  
 }  
  
// This class declares an object of type LegoBlock.  
public class Main {  
  public static void main(String args[]) {  
    LegoBlock myLegoBlock = new LegoBlock();  
    double vol;  
  
    // assign values to myLegoBlock's instance variables  
    myLegoBlock.width = 10;  
    myLegoBlock.height = 20;  
    myLegoBlock.depth = 15;  
  
    // compute volume of box  
    vol = myLegoBlock.width * myLegoBlock.height * myLegoBlock.depth;  
  
    System.out.println("Volume is " + vol);  
  }  
 } 

Each object has its own copies of the instance variables.

For example, the following program declares two LegoBlock objects:

// This program declares two LegoBlock objects.

class LegoBlock {
  double width;//from  w  w  w. j av a  2  s. c  o m
  double height;
  double depth;
}
public class Main {
  public static void main(String args[]) {
    LegoBlock myLegoBlock1 = new LegoBlock();
    LegoBlock myLegoBlock2 = new LegoBlock();
    double vol;

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

    /* assign different values to myLegoBlock2's
       instance variables */
    myLegoBlock2.width = 3;
    myLegoBlock2.height = 6;
    myLegoBlock2.depth = 9;

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

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

The data from myLegoBlock1 is separate from the data contained in myLegoBlock2.




PreviousNext

Related