Class and Object in Java

Objects and Classes are two core concepts of object oriented programming. In OOPs, We design a program using classes and Objects. A programming language is said to be truly Object oriented programming language if everything is represented as Object. Here we will discuss about objects and classes in java programming language.

Points to Remember
  • A class is a template or blueprint which is used to create objects. It specifies the schema of an object by encapsulating the data and functions for manipulating this data in a single entity.

  • Classes provides ability to define your own data types that better correspond to the problem being solved.

  • Classes are extension of structures, like structure a class contains data members but can contains member functions.

  • A java class uses variables to define data fields and methods to define actions.

  • A java class can have fields, methods, static blocks, constructor methods and inner classes.

Syntax of Declaring a Class

class class_name {
    Member Fields;
    Member Methods;
  • Keyword "class" is used in declaration of a class.

  • class_name is any valid java identifiers.

  • Members fields are variables(or object references) which defines the state of an object. These variables are also known as instance variables. Each object of a class will have it's own copy of instance variables.

  • Member methods contains the logic of manipulating the state of the object. Public methods acts as an interface for external world to interact with object.
public class Box {
   float length;
   float width;
   float height;

   float calculateVolume() {

   float getTotalSurfaceArea() {
Naming Conventions of Class Name
  • Class name should be noun. The first letter of class name should be uppercase character. If class name have multiple words then each word will start with uppercase character.
    For Example : Person, Chair, Computer etc.

  • The public class name should be same as the name of the source file which is appended by .java.
    For Example: For a class SmartPhone, the source file name must be

Objects in Java

An object is an programming representation of an real world entity like car, dog, fan etc. All these objects have some state and behavior.

Let's take an example of Car, its; state is color, weight, engine type, manufacturer name etc and it's behaviors are braking, turning, starting and stopping, running etc.

We can model any real world entity in terms of it's state and behaviors. As objects in OOPs represents real world entities, here state of is represented by member variables and behavior is implemented as member methods.

An object is an instance of a class. In other words an object is a variable of type class. We can create as many objects of a class.

Class definition does not allocate any memory, when we create objects of a class then actually memory gets allocated.

Syntax of Declaring an Object

As mentioned above, a class is like blueprint of an object and objects are instances of a class. Keyword "new" is used to create an object of a class. Here is the syntax to create an object:

Class_Name objectReference = new Class_Name();
  • Class_Name is the name of the class for which we are creating object.

  • objectReference can be any valid java identifier.

  • new keyword is used to create an object.

  • new keyword is followed by a call to the constructor method of Class_Name. We can also call parametrized constructor of Class_Name to initialize object by passing some initial value.

For Example:
Box b1 = new Box();