Object and Classes in Java with example

What is an object and classes in Java? How to define a class in java and how to create an object of a class in Java.

In this Java tutorial, Will all the details about the Object and classes that you should know with examples and syntax.

What is an Object?

All around you, you see the objects Like Fan, table, house, room ETC.

Object is an identifiable entity with some characteristics and behavior. Oops, programming objects represent an entity that can store data and has its interface through functions/methods.

An object-oriented program consists of one or many objects each having its own characteristic and behavior. recall that an object has certain details hidden while some of these are exposed due to the property of data abstraction.

while programming using oops oriented approach, Approach the characteristics of an object are represented by its function or methods associated.

Features of objects

All around you, you see the objects. Let’s take the example of a fan in your room is also an object.

It has three characteristics:

  • It has three/four blades.
  • It has a motor.
  • It has a certain color etc.

Its behavior is:

  • It rotates at a specific speed (in rpm).

The following are the four features that are associated with every object you can think of.


These are the properties that characteristics an object e.g., A human being type object has to attribute such as name, age, height, weight, etc.


These are the functionalities associated with an object e.g., A human-being type object has behavior such as speaking, reading writing, sleeping, eating, singing, etc.

Identity of an object:

The unique way of referring to an object e.g., If a human- being type object’s name is Sherin., then to uniquely identify her, we’ll use her identity I.e., Sherin.

State of an object:

It depicts how the object has reacted to the methods applied to it.

What is the Class?

A class is a blueprint that defines similar types of objects.

A class definition identifies all the parameters that define an object that define an object of that particular class. using this class definition, object in the problem domain, you define a class.

Create a class in java

To create/define a class in java, use the keyword class as per the following structure:

<access specifier>class<class name>

The following are the access specifier

Public: Public classes, methods, and fields can be accessed from everywhere. the only constraint is that a file with java source code can only contain one public class whose name must also match with the filename.

Protected: Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes of the same package,

You use the protected access level when it is appropriate for a class’s subclasses to have access to the methods or fields, but not for unrelated classes.

Default: If you do not set access to a specific level, then such a class, methods or fields will be accessible from inside the same package to which the class, methods or fields belongs but not from outside this package.

Private: Private methods and fields can only be accessed within the same class to which the methods and fields belong.

The private methods and fields are not visible within subclasses and are not visible within subclasses and are not inherited by subclasses.so, the private access specifier is opposite to the public access specifier.
It is mostly used for encapsulation: data are hidden within the class and accessor methods are provided.

Attributes in a class

The attributes of an object become fields in the corresponding class. A field within a class consist of the following:

  • Access specifier
  • Data type
  • Class name

What is Method in Java?

The behavior of an object becomes a method in its class definition.

[access-specifier][modifier] return-type method-name(parameter list)
Body of the function

Example of a method in Java,

public class Employee
public string name;//name
public int age;// employee age
public string dept;//department
public double salary;//employee’s salary
public void payslip()
System.out.println(“Paid Rs.” +salary + “to” + name);
}// end of class definition

Instantiating Objects:

In java, the new keyword is used to instantiate an object. The new operator creates the object in memory and returns a reference to the newly created object. this object will remain in memory as long as your program retains a reference to the object.

the following statement declares an Employee reference and uses the new keyword to assign the reference to a new Employee object namely e.

Employee e;
e = new Employee();

The reference e is pointing to the employee object in memory. The new operator allocates memory for the object and then “zeroes” the memory so that none of the objects fields will contain garbage.instead, all fields will have an initial value of zero.