CLASSES AND OBJECTS IN JAVA!
Class is a user-defined data type in Java. It creates a new data type, from which object variables can be declared for holding the object of the new data type. The general form of a class definition in Java is as follows:
class classname{
type instance-variable 1;
type instance-variable 2;
………………
………………
type instance-variable N;
type method 1(parameter-list){
//body of method
}
type method 2(parameter-list){
//body of method
}
………………
………………
type method N(parameter-list){
//body of method
}
}

The variables of a class are called instance variables, because each instance of the class (i.e., each object of the class) contains its own copy of these variables. Thus, the data of one object are separate and unique from the data of another object.
The operations to be performed by an object are defined by the methods of the class. In Java, both declaration and definition (implementation) of the methods belong to a class are written and stored in the same place and are not separated into two. Because having specification, declaration and implementation all in one place makes for code that is easier to maintain.
Object Creation in Java:
- Declare an object variable of type class
- Create a new object of type class using the new keyword and assign its pointer to an object variable.
The first step in object creation is to declare an object variable that will hold the reference to the object to be created. Then create the new object using the class data type and the keyword ‘new’, which will return the pointer to the newly created object. Assign the object pointer to the object variable declared already in Step 1.
The syntax for declaring and creating an object in Java is as follows:
class class-name
{
// members of the class
}
class-name obj_variable = new class-name;
An example for the process mentioned above is as follows:
class Box
{
double width;
double height;
double depth;
}
Box myBox = new Box( );
In this example, Box is a new data type of type class, which contains three instance variables namely width, height, and depth. Using this data type Box, an object variable myBox is declared to hold the reference to an instance of type Box. Now the instance variables width, height, and depth of the newly created object can be accessed using myBox and a dot (.) operator:
myBox.width = 100;
Note that the object creation for myBox is done using the single statement:
Box myBox = new Box( );
This statement can be split into the following two lines of code:
Box myBox;
myBox = new Box( );
The first line declares a variable of type Box, and the second line acquires the actual, physical copy of the object and assigns it to the variable myBox. After the execution of second line, we can make use myBox as if it were a Box object.
But, in reality myBox simply holds the memory address of the actual Box object. The following figure depicts the process involved in object creation.
Statement Effect
myBox = new Box( ); myBox
Box Object
Fig : Declaring and creating an object of type Box