Java Class and Object - Object Creation, Reference and Examples

⏱️ 9 min read • Beginner Level • Lesson 44

Lesson 44 of 124 of Java Tutorial

In Java, a class is a blueprint used to create objects, and an object is a real instance of a class. Classes define data and behavior, while objects use that data and behavior in a program.

Before learning class and objects, you should understand Java OOP concepts, Java methods, and Java variables.


What is Class in Java?

A class in Java is a template or blueprint that defines the properties (data) and behaviors (methods) of objects.

  • A class is used to create objects.
  • Class properties are called fields, attributes, or data members.
  • Class behaviors are defined using methods.
  • Fields and methods together are called class members.
  • A class is declared using the class keyword.
Real-Life Example: A Student class is like a template or blueprint. Every student object created from it can have its own name, roll number, and marks.

Syntax of Class in Java

ClassSyntax.java
Copy Download
class ClassName {
    // fields
    // methods
}

Here, ClassName should be a valid Java identifier. The class body can contain fields, methods, constructors, blocks, and nested classes.

Note: If you do not specify an access modifier, Java uses default access. It is not private by default.

Example: Class with Fields and Method

Test.java
Copy Try Download
class Test {
    int a;
    private int b;
    protected int c;
    public int d;

    void show() {
        System.out.println("show");
    }
}

What is Object in Java?

An object is an entity that has state, behavior, and identity.

Characteristic Meaning Example
State Data or values of an object id, name, salary
Behavior Actions performed by an object showDetails(), deposit(), walk()
Identity Unique identification of an object JVM identifies each object uniquely
Real-Life Example: A car is an object. Its state can be color, model, and speed. Its behavior can be start, stop, accelerate, and brake.

Object Creation in Java

Creating an object from a class is known as object instantiation. It is done using the new keyword.

ObjectCreationSyntax.java
Copy Download
ClassName objectName = new ClassName();
Understanding the Statement:
ClassName objectName = new ClassName();
  • ClassName is the class type of the reference variable.
  • objectName is the reference variable that stores the object's reference.
  • new creates a new object and allocates memory for it in the heap.
  • ClassName() is the constructor call that initializes the object.
  • The reference to the newly created object is assigned to objectName.

What Happens During Object Creation?

  1. JVM allocates memory for the object in the heap.
  2. Instance variables receive default values.
  3. Constructor is executed.
  4. Reference is returned and stored in a variable.
Real-World Example:

A mobile phone company may have a Mobile class. Samsung Galaxy, iPhone, and OnePlus devices are objects created from that class. Each object has its own color, storage, and price.

Example: Object Creation and Member Access

This program creates an Employee object and accesses its fields and method.

Employee.java
Copy Try Download
class Employee {
    int id;
    String name;

    void showDetails() {
        System.out.println(id);
        System.out.println(name);
    }

    public static void main(String[] args) {
        Employee e = new Employee();

        e.id = 123;
        e.name = "Ayan Khan";

        e.showDetails();
    }
}

Output:

123
Ayan Khan

Explanation

  1. Employee is a class.
  2. id and name are instance variables.
  3. showDetails() is an instance method.
  4. Employee e = new Employee(); creates an object.
  5. The object e accesses fields and method using dot . operator.

main() Method in Another Class

In real application development, every class does not need to have a main() method. We can create one class and use it from another class.

MainClass.java
Copy Try Download
class Employee {
    int id;
    String name;

    void showDetails() {
        System.out.println(id);
        System.out.println(name);
    }
}

public class MainClass {
    public static void main(String[] args) {
        Employee e = new Employee();

        e.id = 123;
        e.name = "Ayan Khan";

        e.showDetails();
    }
}

Output:

123
Ayan Khan
Note: In this example, JVM runs MainClass because it contains the main() method.

Object Memory Representation

In Java, the reference variable is stored in stack memory, while the actual object is stored in heap memory.

Java object memory representation
  • Reference variable stores the address/reference of the object.
  • The object is created in heap memory.
  • Each object has its own copy of instance variables.

Instance Variable vs Reference Variable

Instance Variable Reference Variable
Stored inside object Stores object reference
Created in Heap Stored in Stack
Each object gets separate copy Can point to same object

Example: Multiple Objects of Same Class

We can create multiple objects of the same class. Each object has its own copy of instance variables.

MultipleObjectsExample.java
Copy Try Download
class Employee {
    int id;
    String name;

    void showDetails() {
        System.out.println(id);
        System.out.println(name);
    }

    public static void main(String[] args) {
        Employee e1 = new Employee();
        Employee e2 = new Employee();

        e1.id = 123;
        e1.name = "Ayan";

        e2.id = 1234;
        e2.name = "Haaziq";

        e1.showDetails();
        e2.showDetails();
    }
}

Output:

123
Ayan
1234
Haaziq
Multiple object memory representation in Java

Object Alias in Java

An object alias means another reference variable points to the same object. In this case, both references access the same object in heap memory.

ObjectAliasExample.java
Copy Try Download
class Employee {
    int id;

    public static void main(String[] args) {
        Employee e1 = new Employee();
        e1.id = 101;

        Employee e2 = e1; // alias

        e2.id = 202;

        System.out.println(e1.id);
        System.out.println(e2.id);
    }
}

Output:

202
202
Why?

Both e1 and e2 refer to the same Employee object in heap memory. No new object is created. So, any change made through one reference is reflected through the other.

Anonymous Object in Java

An object without a reference variable is called an anonymous object. It is usually used when an object is needed only once.

AnonymousObjectExample.java
Copy Try Download
class Employee {
    void showDetails() {
        System.out.println("Employee details displayed");
    }

    public static void main(String[] args) {
        new Employee().showDetails();
    }
}

Output:

Employee details displayed
Note: Anonymous objects are useful for one-time method calls. Since there is no reference variable, the object cannot be reused directly.

Difference Between Class and Object

Class Object
Class is a blueprint or template. Object is an instance of a class.
No memory is allocated for instance fields when only class is defined. Memory is allocated when object is created using new.
Defines fields and methods. Uses fields and methods.
Example: Employee class. Example: Employee e = new Employee();
Class vs Object in Java

Common Mistakes

  • Thinking class and object are the same.
  • Using object members without creating an object.
  • Forgetting that each object has its own copy of instance variables.
  • Confusing reference variables with actual objects.
  • Using object alias without understanding shared object behavior.

Interview Questions

What is the difference between a class and an object?
A class is a blueprint, while an object is an instance of a class.

Where are objects stored in Java?
Objects are stored in Heap memory.

Where are reference variables stored?
Reference variables are stored in Stack memory.

Summary:
  • A class is a blueprint used to create objects.
  • An object is an instance of a class.
  • Class members include fields and methods.
  • The new keyword creates objects at runtime.
  • Reference variables are stored in stack memory, while objects are stored in heap memory.
  • Multiple objects have separate copies of instance variables.
  • Object alias means two reference variables point to the same object.
  • Anonymous objects are objects without a reference variable.
What Next?

Now that you understand classes and objects, learn about class members, constructors, methods, and access modifiers.

Frequently Asked Questions

A class in Java is a blueprint or template used to create objects. It defines fields and methods.

An object is an instance of a class. It has state, behavior, and identity.

An object is created using the new keyword, for example: Employee e = new Employee();

Object instantiation is the process of creating an object from a class using the new keyword.

Objects are stored in heap memory, while reference variables are usually stored in stack memory.

An object alias means two or more reference variables point to the same object in memory.

An anonymous object is an object that has no reference variable and is usually used for one-time method calls.

Next step: Learn Java Class Members

🚀 Continue to Java Class Members →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Class-and-objects | Language: Java

Q1. What are fields and methods collectively called in a class?
Q2. Where is an object stored in Java memory?
Q3. What will be the output of this code?
class Employee {
    int id;

    public static void main(String[] args) {
        Employee e1 = new Employee();
        e1.id = 101;

        Employee e2 = e1;
        e2.id = 202;

        System.out.println(e1.id);
        System.out.println(e2.id);
    }
}
Q4. What is a class in Java?
Q5. What will be the output of this code?
class Employee {
    int id;
    String name;

    void showDetails() {
        System.out.println(id);
        System.out.println(name);
    }

    public static void main(String[] args) {
        Employee e = new Employee();
        e.id = 123;
        e.name = "Ayan Khan";
        e.showDetails();
    }
}
Q6. What is an object alias in Java?
Q7. What is an anonymous object in Java?
Q8. What is an object in Java?
Q9. Which of the following is the correct syntax to create an object of Employee class?
Q10. Which keyword is used to create an object in Java?

🎉 Great job! Continue learning Java OOP step by step.