Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟣 OOP  ·  Lesson 17

Classes and Objects in Java

In this lesson: Create classes and objects in Java — fields, methods, new keyword and multiple objects — with output. In Hindi & English.

What are classes and objects

Java is object-oriented, so everything is built from classes and objects. A class is a blueprint that describes data and behaviour. An object is a real thing made from that blueprint. "Car" is a class; your specific car is an object.

💡 Class = blueprint (design). Object = a real thing built from it with new.

Defining a class

class Car {
    String color;       // field (data)
    int speed;          // field (data)

    void drive() {      // method (action)
        System.out.println(color + " car driving");
    }
}

A class groups related data (fields like color) and actions (methods like drive). This blueprint does nothing on its own until we create an object from it.

Creating an object

Car c = new Car();      // create an object
c.color = "Red";        // set its data
c.speed = 80;
c.drive();              // call its method
Red car driving

The new keyword builds an object from the class. c holds a reference to it, and we use the dot (.) to set fields and call methods.

Fields and methods

  • Fields (instance variables) store the object's data — color, speed.
  • Methods are what the object can do — drive().
  • You reach both with the dot operator: c.color, c.drive().

Together, fields and methods describe what an object knows and can do.

Many objects, own data

Car a = new Car();  a.color = "Red";
Car b = new Car();  b.color = "Blue";
a.drive();          // Red car driving
b.drive();          // Blue car driving
Red car driving
Blue car driving

Each object made from the class has its own fields. a and b are separate cars with separate colours — changing one does not affect the other.

Methods that use fields

class Student {
    String name;
    int marks;
    void result() {
        String r = (marks >= 40) ? "Pass" : "Fail";
        System.out.println(name + ": " + r);
    }
}
// use:
Student s = new Student();
s.name = "Aman"; s.marks = 75;
s.result();
Aman: Pass

A method can read the object's own fields (via this implicitly). Here result() uses name and marks to decide pass or fail for that student.

Common mistakes

  • Forgetting new. A class is only a blueprint; you need new to make an object.
  • Using fields without an object. Non-static fields belong to an object, reached via the object.
  • File vs class name. A public class must match the file name.
  • Confusing class and object. The class is the design; the object is the actual thing.

Summary

  • A class is a blueprint of fields (data) and methods (actions).
  • An object is a real instance made with new.
  • Access fields and methods with the dot operator.
  • Each object keeps its own copy of the fields.
  • Classes and objects are the foundation of OOP in Java.

Next, set up objects with Java constructors, or reuse code with inheritance.

Frequently Asked Questions

What is the difference between a class and an object?
A class is a blueprint or template that describes what data and behaviour something has. An object is a real thing built from that blueprint. For example, "Car" is a class; your specific red car is an object of it.
How do I create an object in Java?
Use the new keyword with the class name: Car c = new Car();. This creates an object in memory and stores a reference to it in c, which you then use to access its fields and methods.
What are fields and methods in a class?
Fields (also called instance variables) hold an object's data, like a car's colour. Methods are the actions an object can do, like start() or drive(). Together they define what an object knows and can do.
Why does each object have its own data?
Each object created from a class gets its own copy of the fields. So two Car objects can have different colours. Changing one object's data does not affect another, which is the point of objects.
What is object-oriented programming?
Object-oriented programming (OOP) organises code around objects that bundle data and the methods that work on it. Java is object-oriented, so classes and objects are the foundation of every Java program.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicConstructors Your progress: 17 / 61 (28%) · Lesson 18 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free