Classes, Objects, Constructors — Blueprint & Instance

ক্লাস, অবজেক্ট, কনস্ট্রাক্টর — নকশা ও নির্মাণ

Read: ~30 min Intermediate 5 practice problems Live code runner

1. A Class Is a Blueprint, an Object Is the Building

Until now every program you have written lived entirely inside main. That works for scripts, but real Java software is built from classes — user-defined types that bundle state (fields) with behaviour (methods). A class is a blueprint; an object is one concrete building made from that blueprint. You can build hundreds of houses from one blueprint, and each house has its own address, its own residents, its own story — even though the structural plan is identical.

এতদিন আপনি সব কোড main-এর ভেতরেই লিখেছেন। ছোট script-এর জন্য সেটি ঠিক আছে, কিন্তু আসল Java সফটওয়্যার তৈরি হয় class দিয়ে — ব্যবহারকারীর সংজ্ঞায়িত type, যা state (field) এবং behaviour (method) একসাথে রাখে। Class হলো নকশা, আর object হলো সেই নকশা থেকে তৈরি একটি আসল ভবন। এক নকশা থেকে হাজারটি বাড়ি তৈরি সম্ভব — প্রতিটির ঠিকানা, বাসিন্দা, ইতিহাস আলাদা হলেও structural plan একই।

This module introduces class syntax, fields and methods, the new operator, the magical this reference, and how constructors initialise every object you create.

2. Declaring Your First Class

Here is a minimal Student class modelling a Bangladeshi university student. It has two fields (name and CGPA) and one method that prints a greeting. Notice that Main is a separate class that creates Student instances with the new keyword.

নিচে একটি ছোট Student class দেখানো হলো — এতে দুটি field (name, cgpa) এবং একটি method আছে। Main class-এ new keyword দিয়ে Student-এর instance (অবজেক্ট) তৈরি করা হচ্ছে। প্রতিটি object নিজস্ব field-এর মান ধরে রাখে।
Main.java
class Student {
    String name;
    double cgpa;

    void greet() {
        System.out.println("Hello, I am " + name + " (CGPA " + cgpa + ")");
    }
}

class Main {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.name = "Rahim";
        s1.cgpa = 3.75;
        s1.greet();

        Student s2 = new Student();
        s2.name = "Karim";
        s2.cgpa = 3.90;
        s2.greet();
    }
}
Two objects, two independent states. s1 and s2 live in separate memory on the heap; changing s1.name has zero effect on s2.name. Each new allocates fresh storage.

3. What new Actually Does

The new operator asks the JVM to allocate space on the heap for one new object, zero-initialise its fields, run the matching constructor, and return a reference (a handle) to that memory. The variable you assign to lives on the stack — but it only stores the reference, not the object itself.

new আসলে তিনটি কাজ করে — (১) heap-এ নতুন object-এর জন্য জায়গা allocate করে, (২) field-গুলো default value (0, null, false) দিয়ে ভরে, এবং (৩) matching constructor চালায়। ফেরত দেয় একটি reference। আপনার variable stack-এ থাকে এবং সেই reference ধরে রাখে — object নিজে heap-এ থাকে।
Stack (main) Heap s1 → ref @0xAA Student reference s2 → ref @0xBB Student reference Student @0xAA name="Rahim", cgpa=3.75 Student @0xBB name="Karim", cgpa=3.90 Figure 12.1 — stack-এ reference, heap-এ আসল object।

4. Constructors — Initialise at Birth

Setting fields one-by-one after new is error-prone — a caller could easily forget to set one. A constructor lets you require all required state up front. A constructor has the same name as the class and no return type. The this keyword refers to the object currently being initialised, and disambiguates when a parameter has the same name as a field.

Object তৈরি করার পর field এক এক করে সেট করাটা ঝুঁকিপূর্ণ — কেউ ভুলে যেতে পারে। Constructor দিয়ে আপনি জন্মের সময়ই দরকারি সব মান চেয়ে নিতে পারেন। Constructor-এর নাম class-এর নামের সমান, return type থাকে না। this keyword বর্তমান object-কে নির্দেশ করে।
Main.java
class Student {
    String name;
    double cgpa;

    // Constructor — same name as class, no return type
    Student(String name, double cgpa) {
        this.name = name;   // this.name = field; name = parameter
        this.cgpa = cgpa;
    }

    void greet() {
        System.out.println("I am " + name + ", CGPA " + cgpa);
    }
}

class Main {
    public static void main(String[] args) {
        Student s = new Student("Fatima", 3.85);
        s.greet();
    }
}
Default constructor: If you write no constructor at all, Java silently gives you a parameter-less one. The moment you write any constructor yourself, that free default disappears.
ডিফল্ট constructor: কোনো constructor না লিখলে Java নিজেই একটি parameter-less constructor দেয়। কিন্তু নিজে একটি লিখলেই সেই free default আর থাকে না।

5. Overloaded Constructors & this(...) Chaining

You can give a class multiple constructors with different parameter lists — this is called constructor overloading. To avoid duplicating initialisation code, one constructor can call another using this(...) as its very first statement.

একই class-এ একাধিক constructor রাখা যায় — এটাকে constructor overloading বলে। কোড পুনরাবৃত্তি এড়াতে একটি constructor অন্যটিকে this(...) দিয়ে ডাকতে পারে, তবে সেটি constructor-এর প্রথম statement হতে হবে।
Main.java
class Account {
    String holder;
    double balance;

    Account(String holder, double balance) {
        this.holder  = holder;
        this.balance = balance;
    }

    // Chain to the main constructor with a default balance of 0
    Account(String holder) {
        this(holder, 0.0);
    }

    void show() {
        System.out.println(holder + " → BDT " + balance);
    }
}

class Main {
    public static void main(String[] args) {
        Account a1 = new Account("Ayesha", 50000);
        Account a2 = new Account("Rafi");
        a1.show();
        a2.show();
    }
}

6. Vocabulary (শব্দভাণ্ডার)

TermMeaningবাংলায়
classBlueprint from which objects are created.যে নকশা থেকে অবজেক্ট তৈরি হয়।
FieldA variable declared inside a class — per-object state.Class-এর ভেতরে declared ভ্যারিয়েবল — object-এর state।
MethodA function defined inside a class.Class-এর ভেতরে সংজ্ঞায়িত function।
InstanceA concrete object created from a class.Class থেকে তৈরি একটি concrete object।
newOperator that allocates on the heap and calls a constructor.Heap-এ object তৈরি করে constructor চালায়।
thisReference to the current object.বর্তমান object-এর reference।
ConstructorSpecial method that runs when an object is created.Object তৈরির সময় যে বিশেষ method চলে।

7. Practice Problems

Try each problem before peeking at the answer.

প্রতিটি সমস্যা নিজে চেষ্টা করুন, তারপর উত্তর মিলিয়ে নিন। Answer-এর code সরাসরি এই পেজে রান করা যাবে।
  1. Create a Book class with fields title and price, a constructor, and a print() method. Create two books and print them.
    একটি Book class তৈরি করুন যার title ও price field থাকবে, একটি constructor ও print() method থাকবে। দুটি book বানিয়ে print করুন।
    ✨ Show Answer (উত্তর দেখুন)
    Main.java
    class Book {
        String title;
        double price;
        Book(String t, double p) { this.title = t; this.price = p; }
        void print() { System.out.println(title + " — BDT " + price); }
    }
    class Main {
        public static void main(String[] args) {
            new Book("Pather Panchali", 350).print();
            new Book("Lal Shalu", 220).print();
        }
    }
  2. What does System.out.println(new int[3]) print — and why? Explain in two sentences.
    new int[3] print করলে কী দেখায় এবং কেন — দুই বাক্যে ব্যাখ্যা করুন।
    ✨ Show Answer

    Answer: It prints something like [I@1540e19d — that is the default toString of an array: [I means "array of int", followed by the identity hash code. Arrays do not override toString, so you see the raw reference identity; to actually see the values you need Arrays.toString(arr).

    Array toString override করে না — তাই reference identity দেখায়। মান দেখতে Arrays.toString ব্যবহার করুন।

  3. Build a Rectangle class with fields width, height. Add methods area() and perimeter(). Create a 10×4 rectangle and print both.
    একটি Rectangle class বানান — width, height field, area() ও perimeter() method। 10×4-এর একটি rectangle বানিয়ে দুটি মান print করুন।
    ✨ Show Answer
    Main.java
    class Rectangle {
        double width, height;
        Rectangle(double w, double h) { this.width = w; this.height = h; }
        double area()      { return width * height; }
        double perimeter() { return 2 * (width + height); }
    }
    class Main {
        public static void main(String[] args) {
            Rectangle r = new Rectangle(10, 4);
            System.out.println("Area=" + r.area() + ", Perimeter=" + r.perimeter());
        }
    }
  4. Write a Circle class with one constructor taking a radius, and an overloaded constructor with no arguments that defaults the radius to 1. Use this(...).
    একটি Circle class লিখুন যার একটি constructor radius নেয়, আরেকটি argument ছাড়া (default radius 1)। this(...) ব্যবহার করুন।
    ✨ Show Answer
    Main.java
    class Circle {
        double radius;
        Circle(double r) { this.radius = r; }
        Circle()          { this(1.0); }
        double area() { return Math.PI * radius * radius; }
    }
    class Main {
        public static void main(String[] args) {
            System.out.println(new Circle().area());
            System.out.println(new Circle(5).area());
        }
    }
  5. Explain in 2–3 sentences why declaring your own constructor removes Java's default no-arg constructor.
    ব্যাখ্যা করুন — নিজে constructor লিখলে Java-র default no-arg constructor কেন আর পাওয়া যায় না।
    ✨ Show Answer

    Answer: Java only inserts a no-arg default when the class has zero explicit constructors — it assumes you wanted any object to be constructible with no arguments. The moment you declare even one constructor, Java interprets that as "the class author is now in charge of construction" and stops auto-generating anything. If you still want the no-arg flavour, write it yourself explicitly.

    Java তখনই default no-arg constructor দেয় যখন আপনি একটিও constructor লেখেননি। একটিও লিখলে Java ধরে নেয় class author নিজেই construction সামলাচ্ছেন — তখন আপনি চাইলে no-arg constructor আলাদা করে নিজেই লিখে দিতে হবে।

Summary — Module 12

A class is a blueprint; an object is an instance created from that blueprint by the new operator. Objects live on the heap; variables on the stack hold references to them. A constructor runs the moment an object is born, is named after the class, has no return type, and can be overloaded — constructors can even chain to one another with this(...). Use this inside any instance method to talk about the current object.

Class হলো নকশা, object হলো সেই নকশা থেকে new দিয়ে তৈরি instance। Object heap-এ থাকে, variable stack-এ থাকে এবং reference ধরে রাখে। Constructor object-এর জন্মের সময় চলে, class-এর সমনামী, return type নেই, overload করা যায়, এবং this(...) দিয়ে এক constructor থেকে অন্যটিকে ডাকা যায়।

Next Module → Encapsulation & Access Modifiers — private, public ও data hiding।