Classes, Objects, Constructors — Blueprint & Instance
ক্লাস, অবজেক্ট, কনস্ট্রাক্টর — নকশা ও নির্মাণ
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-এর মান ধরে রাখে।
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();
}
}
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-এ থাকে।
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.
this keyword বর্তমান object-কে নির্দেশ করে।
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();
}
}
ডিফল্ট 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.
this(...) দিয়ে ডাকতে পারে, তবে সেটি constructor-এর প্রথম statement হতে হবে।
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 (শব্দভাণ্ডার)
| Term | Meaning | বাংলায় |
|---|---|---|
class | Blueprint from which objects are created. | যে নকশা থেকে অবজেক্ট তৈরি হয়। |
| Field | A variable declared inside a class — per-object state. | Class-এর ভেতরে declared ভ্যারিয়েবল — object-এর state। |
| Method | A function defined inside a class. | Class-এর ভেতরে সংজ্ঞায়িত function। |
| Instance | A concrete object created from a class. | Class থেকে তৈরি একটি concrete object। |
new | Operator that allocates on the heap and calls a constructor. | Heap-এ object তৈরি করে constructor চালায়। |
this | Reference to the current object. | বর্তমান object-এর reference। |
| Constructor | Special method that runs when an object is created. | Object তৈরির সময় যে বিশেষ method চলে। |
7. Practice Problems
Try each problem before peeking at the answer.
-
Create a
Bookclass with fieldstitleandprice, a constructor, and aprint()method. Create two books and print them.একটিBookclass তৈরি করুন যারtitleওpricefield থাকবে, একটি constructor ওprint()method থাকবে। দুটি book বানিয়ে print করুন।✨ Show Answer (উত্তর দেখুন)
Main.javaclass 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(); } } -
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 defaulttoStringof an array:[Imeans "array of int", followed by the identity hash code. Arrays do not overridetoString, so you see the raw reference identity; to actually see the values you needArrays.toString(arr).Array
toStringoverride করে না — তাই reference identity দেখায়। মান দেখতেArrays.toStringব্যবহার করুন। -
Build a
Rectangleclass with fieldswidth,height. Add methodsarea()andperimeter(). Create a 10×4 rectangle and print both.একটিRectangleclass বানান — width, height field, area() ও perimeter() method। 10×4-এর একটি rectangle বানিয়ে দুটি মান print করুন।✨ Show Answer
Main.javaclass 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()); } } -
Write a
Circleclass with one constructor taking a radius, and an overloaded constructor with no arguments that defaults the radius to 1. Usethis(...).একটিCircleclass লিখুন যার একটি constructor radius নেয়, আরেকটি argument ছাড়া (default radius 1)।this(...)ব্যবহার করুন।✨ Show Answer
Main.javaclass 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()); } } -
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.
new দিয়ে তৈরি instance। Object heap-এ থাকে, variable stack-এ থাকে এবং reference ধরে রাখে। Constructor object-এর জন্মের সময় চলে, class-এর সমনামী, return type নেই, overload করা যায়, এবং this(...) দিয়ে এক constructor থেকে অন্যটিকে ডাকা যায়।