Class and Object in Java with Example | How to Create a Class and Object in Java?

The class is the core of Java. It is the logical entity upon which the entire Java language is built because it defines the shape and nature of an object. As such the class is an important part of object-oriented programming in Java. Any concept you want to implement in a Java program must be encapsulated within a class.

In this tutorial, we will be discussing the Classes and Objects in Java, the Difference between Classes and Objects in Java with Examples.

What is a Class?

The most important thing to understand about a class is that it is used to defines a new data type. Once we defined, this new data type can be used to create objects of that type. Thus, we can say that a class is a template for an object, and an object is an instance of a class. A class in Java contains methods, fields, constructors, blocks, and interfaces.

A class can have any of the following variable types.

Local Variable: Variables defined within Blocks, Methods, Constructors are known as Local Variables. Variable is declared and initiated within the Method and destroys after the Method Completes.

Instance Variable: Instance Variables are the Variables within the Class but outside a method. These Variables are initialized whenever the class is instantiated. We can access the Instance Variables inside any method, blocks, or constructors of the particular Class.

Class Variable: These are the Variables declared within a Class and Outside any Method using a Static Keyword.

How to Create a Class?

We can create a class by use of the class keyword. A general form of a class definition is shown here:

class classname{
type instance-variable1;
type instance-variable2;

//-----
type instance variable n;

type methodname1(parameter-list){
//body of method
}

type methodname2(parameter-list){
//body of method
}

//----
type methodnameN(parameter-list){
//body of method
}
}

The data or variables defined inside the class are called instance variables. The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class and the Variables defined within a class are called instance variables Thus, the data for an object is separate and unique from the data for another.

Access Members of a Class

You can use the name of objects along with a . Operator in order to access members of a class.

Members of a Class Example

In the above example, we created a class name Bicycle and includes a field name gear and method called braking().

  • sportsBicycle.gear – access the field gear
  • sportsBicycle.braking() – access the method braking()

What is an Object?

The Object is an entity that has a state and behavior. The object is an instance of a class and an object is a real-world entity. To access the members who are defined in the class you need to create an object.
for example, a chair, pen, table, bike, book, etc are the example of Object.

Creating an Object

As we know Class Provides the Blueprints for Objects. Thus, an object is created from a Class. We need to follow certain steps in order to create an Object and they are as under

Declaration: Declaration of a Variable with a Variable Name and an Object Type.

Instantiation: A ‘new’ keyword is used to create an object.

Initialization: The ‘new’ keyword is followed by a call to the constructor. Doing so initializes the New-Object.

How to Create an Object in Java?

We can create an object with the help of a new operator. The new operator is used to dynamically allocates memory for an object and return a reference to it.
for example: Box obj = new Box();

Here we can create an object of Box class and obj is a reference to an object of type Box.

Class and Object in Java with Example

Creating an Object Example

Creating an Object Example Program

Output

Passed Name is :tommy

Characteristics of Object

Following are the three characteristics of Object in Java. They are namely State, Behavior, Identity.

State: It denotes the data value of an object.

Behavior: It represents the functionality or behavior of an object such as withdraw, deposit, etc.

Identity: Usually, Object Identity is implemented via a Unique ID. However, the value of the ID is not shown to the end-user but is stored in the JVM internally to identify each object uniquely.

Characteristics of Object

Class and Object in Java with Example Program

In this example, we create a class called Box that defines three instance variables: width, height, and depth. Currently, Box does not contain any methods. In this example, we compute the volume of a box.

class Box{
//declare member variables
double width;
double height;
double depth;
}
class BoxVolume{
public static void main(String args[]){

//creating an object of Box class
Box obj = new Box();
double vol;

//assign values to obj instance variable
obj.width = 10;
obj.height = 20;
obj.depth = 30;

//compute volume of Box
vol = obj.width * obj.height * obj.depth;

//printing the volume of Box
System.out.println("Volume of Box is: " +vol);
}
}

The following example computes the volume of a box which is 6000.0 in this case.

Leave a Comment