Java Class | Java Object

Class and Object in Java Class and Object in Java

In this tutorial, We will discuss about Class and Object in Java. What are the different ways of creating Objects in Java. What’s happening internally when you create a New object of a class. These are the basics things which should be clear in Java programming.

What is Class

To understand what Class actually is, let’s take a real world example.

In our day to day life, we see different bikes like harley, suzuki, bajaz etc. They are made by different manufacturers like harley is prepared by Harley-Davidson, Inc, Pulser is prepared by Bajaz. We know, these manufacturers have their own production house, where they prepare thousands of bikes per day.

They prepare thousands of bikes per day, how is it possible. How they prepare each bike of same structure, same look and same color etc. Reason is, they first prepare a model or blueprint of that bike. And blueprint means, size, colors, tyres, gears and other different parts of bike. Company first prepare Blueprint for different models like Harley street 750, Harley street 800 etc.

When blueprint is ready, then company can now start production. Company need to follow that blueprint and prepare the bikes as per that model or blueprint.

And that is simply called a “Class” in java. A Class in java is simply a blue print or model which needs to be followed.

Syntax of a Class

A basic syntax of a class is as below :

class ClassName {

     // Member declaration

     // Method Declaration


A simple example

class DemoClass {
      int a = 10;

      public void showA() {
         System.out.println("Value of a : " + a);

What is Object in Java

Let’s extend the above example of bike and understand what is Object in java.

Now we know that company needs to prepare a blueprint for a particular model so that they can start production of bikes. By following blueprint, company prepares thousands of bikes per day and that is called “Object” which is real.

Blueprint says the bike will look like this, this will be the speed of bike and that would be the color of the bike. But blueprint is not real, you need to prepare an “Object” out of it.

So an “Object” is the instance of the “Class” which physically exist and which is real. An Object has state and behavior. An Object can perform actions and can store something.

What is Instance Variable

We before create class and objects, we first need to understand some basics about a class and objects. An instance variable is used by an Object to store it’s state. for example

class Demo {
      int a = 10; // Instance variable

What is Reference Variable

A reference variable is declared with a specific type which can not be change. A reference variable is used to access a particular object. Without reference variable, you will not be able to access an object and it’s properties.

Reference variable is just like a pointer to the memory location where Object resides.

Demo obj = new Demo();
//obj is now reference variable of Demo type class. which can not be change.

What are methods in Java

Methods are the behavior of an Object. For example, let’s take an example of Bike object. Bike can run, stop, can change different gears, can move left, right. These some basic behavior of a bike.

Same way, we define methods in Java to provide behavioral information of a particular Object.

Syntax of a method in Java

Access_Modifiers Non_Access_Modifer return_type methodName(paremeter list) {
      // Method body

We will discuss about access modifiers and non access modifiers later. But for now we can say that access modifiers and non access modifiers tells how a particular method should be accessed and behave outside of a class.

A Simple Class and Object example

Let’s take a look on below example. This is how we create a Class, Object and access the properties of an object.

Class and Object in Java
Class and Object in Java

Let’s check what’s going on internally, when we create a new Object of a class.

First we create a reference variable of a particular type, in our case we are creating a reference variable of DemoClass type, where DemoClass is a class.

You can not access anything until an object is assigned to the reference variable. If you will try to do so, exception will be thrown by the system. Without object, a reference variable is just like empty box.

After that, new keyword is detected by JVM. Always remember, when there is a new keyword, memory will always be assigned in Heap Area of JVM. Object always reside in Heap area.

New keyword checks, what is Class type for which Object will be created. If the type is proper, then space will be created in Heap area of JVM and Object id will be assigned to the reference variable.

Now reference variable will be able to access the Objects fields and methods.

Leave a Reply

Your email address will not be published. Required fields are marked *