Java📅 Day 10

Class & Object in Java

Define a class, create objects, use constructors and access instance members — the heart of OOP.

🎯

Learning objectives

  • Define a class with fields, constructors and methods
  • Create objects using new and call instance methods
  • Understand this, and the role of a constructor

💡 Key points

  • Class = blueprint. Object = instance created from that blueprint.
  • Every object has its own copy of instance variables.
  • Constructor: special method with the class name, no return type. Runs when object is created.
  • this refers to the current object; used to disambiguate fields from parameters.

💻 Code examples(4)

#1Define a class
java
public class Student {
    // fields (state)
    String name;
    int rollNo;
    double marks;

    // constructor
    public Student(String name, int rollNo, double marks) {
        this.name = name;
        this.rollNo = rollNo;
        this.marks = marks;
    }

    // instance method (behaviour)
    public void printReport() {
        System.out.println(rollNo + " " + name + " → " + marks);
    }
}
Save as Student.java. Fields = data. Constructor = how to build one. Methods = what it can do.
#2Create and use objects
java
public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Amit",  1, 87.5);
        Student s2 = new Student("Priya", 2, 92.0);

        s1.printReport();
        s2.printReport();

        System.out.println(s1.name);  // Amit
    }
}
Output
1 Amit → 87.5
2 Priya → 92.0
Amit
new Student(...) allocates memory + calls the constructor. Access members with the dot operator.
#3Multiple constructors (overloading)
java
public class Book {
    String title;
    double price;

    public Book(String title) {
        this(title, 0.0);            // call the other constructor
    }

    public Book(String title, double price) {
        this.title = title;
        this.price = price;
    }
}
this(...) chains constructors — must be the first statement. Helps avoid duplicated init code.
#4Default vs no-arg constructor
java
public class Point {
    int x, y;
    // No constructor written — Java adds a default no-arg one automatically.
}

Point p = new Point();   // x=0, y=0
If you don't declare any constructor, Java gives you a public no-arg one. Once you write ANY constructor, the default disappears.

🎯 Practice

Q1. What is the difference between a class and an object?+

A class is a blueprint (definition). An object is a concrete instance created from that blueprint. One class → many objects.

Q2. Why do we need `this` inside a constructor?+

When parameter names match field names, `this.field` refers to the object's field and `field` alone refers to the parameter. `this` disambiguates them.

Q3. Define a class Rectangle with width, height, a constructor, and an area() method.+

class Rectangle { double width, height; Rectangle(double w, double h) { this.width = w; this.height = h; } double area() { return width * height; } }

📝 Notes

The 4 pillars of OOP (preview)

| Pillar | One-line meaning | |---|---| | Encapsulation | Bundle data + methods; hide internals with private | | Inheritance | New class reuses fields/methods of an existing class | | Polymorphism | Same method behaves differently for different types | | Abstraction | Expose what an object does, hide how |

Today's chapter is the foundation — every real-world Java program is a collection of classes talking to each other via their objects.

Common beginner mistake

Student s;              // just a reference, currently null
s.printReport();        // NullPointerException — no object yet!

Always initialize with new before calling methods on a reference.