Java Class

Description

A class defines a new data type.

This new type can be used to create objects of that type.

A class is a template for an object, and an object is an instance of a class.

Syntax

The general form of a class definition is shown here:


class classname { 
  type instance-variable1; //  w w  w .  j  ava2s .c  om
  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 
  } 
}

A class is declared by using the class keyword.

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

Variables defined within a class are called instance variables because each instance of the class contains its own copy of these variables.

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

Example

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

 
class Box { //from   w w w .j ava2 s .  co  m
    int width; 
    int height; 
    int depth; 
}




















Home »
  Java Tutorial »
    Java Language »




Java Data Type, Operator
Java Statement
Java Class
Java Array
Java Exception Handling
Java Annotations
Java Generics
Java Data Structures