Java Classes and Objects

What is a class in Java?

Classes are a construct on which the entire Core Java is built. A class is the basis of object-oriented programming in Java. In this tutorial, we will learn about Classes and Objects in Java. In Java, class is a template for an object, whereas an object is an instance of a class. Now, you would be thinking, why? Well, here’s the reason:

Class defines a new data type, which after defining can be used to create objects of that type. This is why, class is a template for an object and object an instance of class.

Class Definition

At first, to declare a class, use the class keyword in Java. The class is a reserved word in Java. Let us now see how to define a class in Java:

In the above syntax, the <yourclassname> is the name of the class user needs to set. Before going further, let us first look into the terms used with class. The <instance-var1>, <instance-var1>, etc. are the instance variables. <methodname1>, <methodname2>¸etc are the name of the methods.

Instance Variables

Variables defined in a class are Instance Variables. Each instance (object) of the class has its own copy of these variables. Example is shown below.

Class Members

Variables and methods in a class are Class Members

What is an Object in Java?

An object is an instance of a class i.e. object is created from a class. Object represents real-life entities, for example, a Bike is an object. Object has State, Behavior, and Identity:

  • Identity: Name of Bike
  • State (Attributes) : The data of object Bike i.e. Color, Model, Weight, etc.
  • Behavior (Methods): Behavior of object Bike like to Drive, Brake

Let us now see the representation, with Bike as an object:Classes and Objects in Java

Therefore, Java is all about classes and objects, with the attributes and methods.

Creating a class in Java

Let us see an example wherein we will create a class and understand Instance Variables and Class Members concept.

We have a class here, “Rectangle” with two instance variables:

Creating object in Java

An object is a runtime entity with state and behavior. Object also has identity, which isn’t revealed to the user. To create an object in a class, use the new operator. Let us see the above example and create an object rct:

The output is as follows:

Above, we have created an object rct using new operator. This dynamically allocates memory for an object:

Let’s see the below representation. We have object rct:

Understanding Java Objects

The rct above is now an instance of Rectangle. Now, every Rectangle object will have its own copy of the instance variables length and width. We used the dot operator above to access the instance variables:

Above, the copy of instance variables length and width contained in the object rct are assigned values using the dot operator.

Creating multiple objects

We saw above how to create an object and its relation with instance variables in a class. With Java, we can create multiple objects in the same class. Let us see an example demonstrating multiple objects and showing each instance (object) of the class has its own copy of instance variables:

The code is as follows to create multiple objects in a single class; each object having its own copy of instance variables:

The output is as follows:

Let us now see below, we have two objects rct1 and rct2. Each of these objects have their own copy of instance variables length and width:

Object and Instance Variables

Above, we have created two objects rct1 and rct2. Then, we assigned values to each object with its own copy of instance variables i.e. for rct1:

For object rct2:

Let us now see another example, wherein both rct1 and rct2 will refer to the same object. We will perform assignment on object reference variables:

The output is as follows:

In the above code, at first, we created a single object:

After that, by assigning, we referred both to the same object:

Adding Methods

We will add a method to the class Rectangle created above. The instance variables will be accessed in this method and the area of rectangle will be calculated:

The output is as follows:

In this tutorial, we saw how work with classes and objects in Java. We also saw how to create classes and object, and the concept of instance variables, class members, etc.

Download and Install Java 14 on Windows 10
Constructors in Java with Examples
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading