Java – Class and Object

Prev     Next

In object oriented programming languages, classes and objects are the basic elements of a program. Messages are passed only through objects.


An object is an entity, which possess some properties and behavior. Example – Person is an object with properties – Name, Gender, etc and with behavior – sleeping, eating etc.


 A class is a blueprint which defines the properties and behaviour of the object which it supports

Class in Java:

A group of objects having similar properties and behaviour are enclosed within a class.


The class ‘Bird’ would have the properties and behaviour which are common to all its objects – peacock, parrot etc.


Properties and behaviour of objects are referred to as data members (fields/variables) and methods respectively.


class class_name


Objects in Java:

Object is an instance of class. In other words, they are variables of the type class.

Every object has,

  • State – State represents the property of the object.
  • Behaviour – Behaviour represents the functionality of the object.
  • Identity – It is an unique id defined by the JVM (internally) to refer the object.

Creating an object:

Once a class is created, objects of that class are created by using it’s class name.


class_name object = new constructor();


Employee e1 = new Employee();

  • The object ‘e1’ is created and instantiated using the new keyword.
  • The new keyword is followed by a call to the constructor which initializes the object.
  • The name of the constructor is same as that of the class name. Constructors would be discussed later.

Accessing the fields and methods of a class:

The fields and methods of a class can be accessed by using the ‘dot’ operator.





  • Objects are anything that exists in the world. A Class is a blueprint to develop objects.
  • If you consider creating objects like chair, table, cupboard, then you need to create class called Furniture.
  • A class contains variables and methods.
  • Variables are attributes or properties.
  • Method perform functions or actions.

A Class looks like

Example program to understand objects :


C:\> javac
C:\> java UsePerson
Im Dara
Im 20 years old

We have two classes in a same source file. Then, which name should we use as a file name?

You have to use the name of the class containing main() method. so we save the above source file as

The main() method:

A Java application can have any number of classes but atleast one class should contain a main() method. During runtime, the JVM will search for the main() method and start executing the statements in main() method.

The syntax is,

public static void main(String [] args)
// statements

The new() operator:

Using Person class, you can create any number of objects of type Person using new() operator.The syntax is,

classname objectname = new classname();

To create an object for Person class, we use

Person personOne= new Person();

new Person() will create a object of type Person.
personOne is a variable name referring to the Person object.

The dot operator:

The variables and methods of Person class is available to all the objects of Person class. Those variables and method are accessed by using dot operator.

The name variable of personOne object is specified by = “Dara”;

similarly, the age variable of personOne object is specified by personOne.age

personOne.age= 20;

Prev     Next

Like it? Please Spread the word!