ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
ဘဏ်မှာ account အသစ်ဖွင့်တဲ့အခါ form ဖြည့်ရပါတယ် — နာမည်၊ မှတ်ပုံတင်နံပါတ် စတာတွေ မပါရင် ဝန်ထမ်းက account ကို မဖွင့်ပေးပါဘူး။ Constructor ဆိုတာ object တစ်ခုအတွက် အဲဒီ "ဖွင့်လှစ်ခြင်း form" ပါပဲ။ new Course("Java Basics", 12) လို့ ရေးလိုက်တာနဲ့ constructor က အလိုအလျောက် run ပြီး လိုအပ်တဲ့ data တွေကို field ထဲ ထည့်ပေးပါတယ်။
Constructor ရဲ့ နာမည်က class နာမည်နဲ့ တူရပြီး return type လုံးဝ မရေးရပါဘူး၊ void တောင် မရေးရပါဘူး။ ဒါကြောင့် object တစ်ခုက တစ်ဝက်တစ်ပျက် အခြေအနေ (title မရှိတဲ့ course လို) နဲ့ မွေးလာစရာ မလိုတော့ပါဘူး — ဆောက်ပြီးတာနဲ့ သုံးလို့ရတဲ့ အခြေအနေ ဖြစ်နေပါပြီ။
Problem → Why → Solution
ပြဿနာ: field တွေ သတ်မှတ်ဖို့ မေ့တဲ့ object
Constructor မရှိရင် object ဆောက်ပြီးမှ field တစ်ခုချင်းကို လိုက် assign လုပ်ရပါတယ်။ Field တစ်ခု မေ့သွားရင် compiler က ဘာမှ မပြောဘဲ null ဒါမှမဟုတ် 0 နဲ့ ဆက်သွားပါတယ်။ အောက်က code မှာ ဒုတိယ course အတွက် title ကို မေ့သွားလို့ "null has 8 lessons." ထွက်ပါတယ်။ ပိုဆိုးတာက constructor ရေးချင်ပေမယ့် void Course(...) လို့ ရေးမိရင် constructor မဟုတ်ဘဲ ရိုးရိုး method ဖြစ်သွားပါတယ်။
public class Main {
public static void main(String[] args) {
Course javaCourse = new Course();
javaCourse.title = "Java Basics";
javaCourse.lessons = 12;
Course sqlCourse = new Course();
sqlCourse.lessons = 8; // forgot sqlCourse.title
sqlCourse.showInfo(); // null has 8 lessons.
}
}
class Course {
String title;
int lessons;
void Course(String t, int n) { // void: this is a METHOD, not a constructor
title = t;
lessons = n;
}
void showInfo() {
System.out.println(title + " has " + lessons + " lessons.");
}
}ဘာကြောင့် ဒီလို ဖြစ်တာလဲ
Object ဆောက်တာနဲ့ field သတ်မှတ်တာက သီးခြား အဆင့်နှစ်ခု ဖြစ်နေလို့ ကြားထဲမှာ မပြည့်စုံတဲ့ object တစ်ခု ရှိနေပါတယ်။ Constructor ထဲမှာ parameter အဖြစ် လိုအပ်တဲ့ data ကို တောင်းလိုက်ရင် new Course() လို data မပါတဲ့ call က compile error ဖြစ်သွားပြီး မပြည့်စုံတဲ့ object မွေးဖို့ လမ်း ပိတ်သွားပါတယ်။ void ထည့်မိရင်တော့ Java က method အဖြစ် ယူပြီး default constructor ကို ဆက်သုံးပါတယ်။
Core concept: constructor က object ရဲ့ ကတိကို စတင်ချမှတ်တယ်
new Course("Java Basics", 12) ကို run တဲ့အခါ Java က object အတွက် memory ယူ၊ field တွေကို default value (0, null) ပေး၊ ပြီးမှ ကိုက်ညီတဲ့ constructor ကို run ပါတယ်။ Constructor ပြီးဆုံးမှသာ reference ကို caller ဆီ ပြန်ပေးပါတယ်။ ဒါကြောင့် constructor ဟာ "title ဗလာ မဖြစ်ရ"၊ "price အနုတ် မဖြစ်ရ" လို object တစ်ခုရဲ့ သက်တမ်းတစ်လျှောက် မှန်နေရမယ့် စည်းမျဉ်း (invariant) တွေကို စစ်ဖို့ အကောင်းဆုံး နေရာပါ။ စည်းမျဉ်း မကိုက်ရင် IllegalArgumentException ကို throw လုပ်ပြီး object ကို လုံးဝ မမွေးစေပါနဲ့။
Parameter နာမည်ကို field နာမည်နဲ့ တူအောင် ပေးလေ့ရှိပါတယ် (String title)။ အဲဒီအခါ constructor ထဲမှာ title ဆိုရင် parameter ကို ဆိုလိုပြီး field ကို ညွှန်ဖို့ this.title လို့ ရေးရပါတယ်။ this.title = title; က "ဒီ object ရဲ့ title field ထဲကို parameter title ကို ထည့်" လို့ ဖတ်ပါ။
Class တစ်ခုမှာ constructor လုံးဝ မရေးထားရင် compiler က parameter မပါတဲ့ default constructor ကို ထည့်ပေးပါတယ်။ Constructor တစ်ခု ရေးလိုက်တာနဲ့ အဲဒီ default constructor ပျောက်သွားလို့ new Course() က "constructor Course in class Course cannot be applied to given types" error ဖြစ်ပါတယ်။ Overloaded method တွေလိုပဲ parameter list မတူတဲ့ constructor အများကြီး ရေးလို့ရပါတယ်။
Constructor တစ်ခုက တခြား constructor ကို this(...) နဲ့ ခေါ်လို့ရပါတယ် — ဒါကို constructor chaining လို့ ခေါ်ပါတယ်။ Course(String title) က this(title, 0) ကို ခေါ်လိုက်ရင် validation code က parameter အပြည့်အစုံ constructor ထဲမှာပဲ ရှိပြီး ထပ်ခါထပ်ခါ မရေးရတော့ပါဘူး။ Java 21 မှာ this(...) က constructor ရဲ့ ပထမဆုံး statement ဖြစ်ရပါမယ်။ final field တွေကိုတော့ constructor တိုင်းက တစ်ခါတည်း assign လုပ်ရပြီး နောက်ပိုင်း ပြောင်းလို့ မရပါဘူး။
Constructor ဟာ method နဲ့ ဆင်ပေမယ့် method မဟုတ်ပါဘူး။ Object တစ်ခုအတွက် တစ်ကြိမ်ပဲ run ပြီး myCourse.Course(...) လို နောက်မှ ပြန်ခေါ်လို့ မရပါဘူး၊ subclass တွေကိုလည်း ဆက်ခံ (inherit) မပေးပါဘူး။ Object ဆောက်ပြီးနောက် state ပြောင်းဖို့ဆိုရင် addLesson() လို အဓိပ္ပာယ်ရှိတဲ့ method တွေကို သုံးပြီး အဲဒီ method တွေကလည်း constructor ချမှတ်ထားတဲ့ စည်းမျဉ်းတွေကို ဆက်စောင့်ထိန်းရပါမယ်။
- new က memory ယူပြီး default ပေး
- ကိုက်တဲ့ constructor ကို ရွေး
- this(...) ရှိရင် အရင် run
- Validation အောင်မှ field တွေ သတ်မှတ်
- ပြီးမှ reference ကို caller ဆီ ပေး
Constructor က exception throw လုပ်ရင် caller ဆီ object reference လုံးဝ မရောက်ပါဘူး။
အထွေထွေ ပုံစံ
class ClassName {
final Type required;
Type optional;
ClassName(Type required, Type optional) { // no return type
// validate first, throw if invalid
this.required = required;
this.optional = optional;
}
ClassName(Type required) {
this(required, defaultValue); // must be the first statement
}
}Constructor နာမည်က class နာမည်ဖြစ်ပြီး return type မရှိပါ။ Parameter အပြည့်အစုံ constructor က validation နဲ့ assignment ကို လုပ်ပြီး တိုတဲ့ constructor တွေက this(...) နဲ့ default value ဖြည့်ပြီး လွှဲပေးပါတယ်။
Example: validation နဲ့ chaining ပါတဲ့ Course
ဒီ Course class မှာ constructor နှစ်ခု ရှိပါတယ်။ Parameter နှစ်ခုပါ version က title ကို စစ်ပြီး field တွေ သတ်မှတ်ပါတယ်၊ title တစ်ခုတည်းပါ version ကတော့ lesson မရှိသေးတဲ့ draft course အတွက်ဖြစ်ပြီး this(title, 0) နဲ့ လွှဲပေးပါတယ်။ main ရဲ့ နောက်ဆုံးအပိုင်းက title ဗလာနဲ့ ဆောက်ဖို့ ကြိုးစားပါတယ်။ try/catch ကို Exceptions lesson မှာ အသေးစိတ် လေ့လာမှာဖြစ်ပြီး ဒီမှာတော့ program မရပ်ဘဲ error message ကို print ထုတ်ဖို့ပဲ သုံးထားပါတယ်။
public class Main {
public static void main(String[] args) {
Course javaCourse = new Course("Java Basics", 12);
javaCourse.showInfo();
Course draft = new Course("Kotlin Basics");
draft.showInfo();
try {
new Course(" ", 5);
} catch (IllegalArgumentException e) {
System.out.println("Rejected: " + e.getMessage());
}
}
}
class Course {
final String title;
int lessons;
Course(String title, int lessons) {
if (title == null || title.isBlank()) {
throw new IllegalArgumentException("title is required");
}
this.title = title;
this.lessons = lessons;
}
Course(String title) {
this(title, 0); // a draft course starts with no lessons
}
void showInfo() {
System.out.println(title + " has " + lessons + " lessons.");
}
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Constructor header
Course(String title, int lessons) {Class နာမည် Course နဲ့ တူပြီး return type မပါလို့ constructor ဖြစ်ပါတယ်။ void Course(...) လို့ ရေးရင် method ဖြစ်သွားပါမယ်။
- Guarding the invariant
throw new IllegalArgumentException("title is required");Title က null ဒါမှမဟုတ် space တွေပဲ ဖြစ်ရင် exception throw လုပ်လို့ title မှားတဲ့ Course object တစ်ခုမှ program ထဲ မရောက်နိုင်ပါဘူး။ isBlank() က Java 11 ကစပြီး ရှိပါတယ်။
- Field vs parameter
this.title = title;ဘယ်ဘက်က this.title က field၊ ညာဘက်က title က parameter ပါ။ this. ကို ဖြုတ်လိုက်ရင် parameter ကို သူ့ကိုယ်သူ ပြန် assign လုပ်ရုံပဲ ဖြစ်ပြီး final field မို့ compile error လည်း တက်ပါမယ်။
- Constructor chaining
this(title, 0); // a draft course starts with no lessonsParameter နှစ်ခုပါ constructor ကို ခေါ်လို့ blank-title validation က draft course အတွက်လည်း အလုပ်လုပ်ပါတယ်။ ဒီ line က constructor ရဲ့ ပထမဆုံး statement ဖြစ်ရပါမယ်။
- Choosing a constructor
Course draft = new Course("Kotlin Basics");Argument တစ်ခုပဲ ပါလို့ compiler က overload ရွေးသလိုပဲ Course(String) ကို ရွေးပါတယ်။ ရလဒ်က "Kotlin Basics has 0 lessons." ပါ။
Java Basics has 12 lessons.
Kotlin Basics has 0 lessons.
Rejected: title is requiredRecord ရဲ့ compact constructor
Record တစ်ခုမှာ component တိုင်းကို parameter အဖြစ် ယူပြီး field တွေကို assign လုပ်ပေးတဲ့ canonical constructor ကို compiler က အလိုအလျောက် ထုတ်ပေးပါတယ်။ Validation ထည့်ချင်ရင် parameter list မပါတဲ့ compact constructor ကို ရေးပါ — စစ်ဆေးမှု code ကိုပဲ ရေးပြီး this.amount = amount လို assignment တွေကို compiler က နောက်ကနေ ထည့်ပေးပါတယ်။ ငွေပမာဏ၊ ကိုဩဒိနိတ်၊ date range လို ပြောင်းစရာမလိုတဲ့ value type တွေအတွက် ဒီပုံစံက အတိုဆုံးနဲ့ အဘေးကင်းဆုံး ဖြစ်ပါတယ်။
public class Main {
public static void main(String[] args) {
Money price = new Money(2500, "MMK");
System.out.println(price);
System.out.println(price.amount() + " " + price.currency());
try {
new Money(-1, "MMK");
} catch (IllegalArgumentException e) {
System.out.println("Rejected: " + e.getMessage());
}
}
}
record Money(long amount, String currency) {
Money { // compact constructor: no parameter list
if (amount < 0) {
throw new IllegalArgumentException("amount must not be negative");
}
}
}Output က Money[amount=2500, currency=MMK]၊ 2500 MMK နဲ့ Rejected: amount must not be negative ဖြစ်ပါတယ်။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
User account ဆောက်တဲ့အခါ email နဲ့ username၊ Product ဆောက်တဲ့အခါ name နဲ့ price လို မဖြစ်မနေ လိုအပ်တဲ့ data တွေကို constructor parameter အဖြစ် တောင်းပါတယ်။ ဒါမှ "email မရှိတဲ့ user" ဆိုတဲ့ အခြေအနေကို code ရဲ့ ဘယ်နေရာမှာမှ စစ်စရာ မလိုတော့ပါဘူး — constructor က အာမခံပြီးသားပါ။ Domain-driven design မှာ ဒါကို "always-valid object" လို့ ခေါ်ပြီး bug အမျိုးအစား တစ်ခုလုံးကို ဖယ်ရှားပေးပါတယ်။
Spring လို framework တွေမှာ constructor injection က standard ပုံစံ ဖြစ်ပါတယ် — OrderService(PaymentGateway gateway, OrderRepository repo) လို constructor ကနေ dependency တွေကို ယူပြီး final field တွေထဲ သိမ်းပါတယ်။ ဒါကြောင့် service object တိုင်းမှာ dependency အားလုံး ရှိပြီးသား ဖြစ်ပြီး test ရေးတဲ့အခါ fake gateway တစ်ခုကို constructor ကနေ ထည့်ပေးလိုက်ရုံပါပဲ။ Optional field များတဲ့ class တွေအတွက်တော့ constructor parameter ဆယ်ခုလောက် ဖြစ်မလာအောင် builder pattern ကို သုံးလေ့ရှိပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Constructor ကို void နဲ့ ရေးတာ
void Course(String t, int n) က method ဖြစ်လို့ new Course("Java", 12) က "constructor Course in class Course cannot be applied to given types" ဖြစ်ပါတယ် — compiler က ထည့်ပေးထားတဲ့ default constructor က argument မယူလို့ပါ။ void ကို ဖြုတ်ပါ။
2. this. မေ့ပြီး parameter ကို သူ့ကိုယ်သူ assign လုပ်တာ
Course(String title, int lessons) { title = title; lessons = lessons; } က compile ဖြစ်ပေမယ့် field တွေကို လုံးဝ မထိပါဘူး၊ ဒါကြောင့် "null has 0 lessons." ထွက်ပါတယ်။ this.title = title; လို့ ရေးပါ၊ field ကို final ထားရင် ဒီအမှားကို compiler က ဖမ်းပေးပါတယ်။
3. Constructor တစ်ခုထည့်ပြီး new X() ဆက်သုံးတာ
Constructor တစ်ခု ရေးလိုက်တာနဲ့ default constructor ပျောက်သွားလို့ အရင်က အလုပ်လုပ်ခဲ့တဲ့ new Course() တွေ အကုန် compile error ဖြစ်ပါတယ်။ တကယ်လိုရင် no-argument constructor ကို ကိုယ်တိုင် ထပ်ရေးပြီး this(...) နဲ့ default value ပေးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Validation ကို parameter အပြည့်အစုံ constructor တစ်ခုထဲမှာပဲ ထားပြီး ကျန်တာတွေကို this(...) နဲ့ ချိတ်ပါ။
- ပြောင်းစရာ မလိုတဲ့ field တွေကို final လုပ်ပါ။
- Constructor ထဲမှာ object ကို setup လုပ်ရုံပဲ လုပ်ပါ — network call, file ဖတ်တာလို လေးလံတဲ့ အလုပ်တွေ မလုပ်ပါနဲ့။
Mini challenge
Mini challenge: validation ပါတဲ့ Product
ဆိုင်တစ်ဆိုင်အတွက် Product class ကို constructor နှစ်ခုနဲ့ ရေးပါ။ မှားယွင်းတဲ့ ဈေးနှုန်း ဒါမှမဟုတ် stock နဲ့ product တစ်ခုမှ မမွေးရပါဘူး။
- Field: final String name, final int priceKyat, int stock
- Product(name, priceKyat, stock) က price <= 0 ဆိုရင် "price must be positive"၊ stock < 0 ဆိုရင် "stock cannot be negative" နဲ့ IllegalArgumentException throw လုပ်ရမယ်
- Product(name, priceKyat) က this(...) နဲ့ stock 0 ပေးပြီး ချိတ်ရမယ်
- describe() က "Green Tea - 1500 MMK, stock 40" ပုံစံ print ထုတ်ရမယ်
public class Main {
public static void main(String[] args) {
// TODO: tea = Green Tea, 1500, 40; cake = Cheesecake, 4500 (no stock given)
// TODO: call describe() on both
// TODO: try a price of 0 and a stock of -2, printing "Rejected: " + message
}
}
class Product {
// TODO: final name, final priceKyat, stock
// TODO: Product(String name, int priceKyat, int stock) with validation
// TODO: Product(String name, int priceKyat) chaining with this(...)
void describe() {
// TODO: "<name> - <price> MMK, stock <stock>"
}
}ရလဒ်: ဒီလို လေးကြောင်း ထွက်ရပါမယ်: Green Tea - 1500 MMK, stock 40 Cheesecake - 4500 MMK, stock 0 Rejected: price must be positive Rejected: stock cannot be negative
အရိပ်အမြွက် ကြည့်ရန်
final field တွေကို validation အောင်ပြီးမှ assign လုပ်ပါ။ Product(String, int) ထဲမှာ this(name, priceKyat, 0); တစ်ကြောင်းတည်း ရှိရပါမယ်။ မှားတဲ့ product တွေကို try { new Product(...); } catch (IllegalArgumentException e) { ... } နဲ့ စမ်းပါ။
အဖြေ ကြည့်ရန်
public class Main {
public static void main(String[] args) {
Product tea = new Product("Green Tea", 1500, 40);
Product cake = new Product("Cheesecake", 4500);
tea.describe();
cake.describe();
try {
new Product("Ghost", 0, 5);
} catch (IllegalArgumentException e) {
System.out.println("Rejected: " + e.getMessage());
}
try {
new Product("Ghost", 900, -2);
} catch (IllegalArgumentException e) {
System.out.println("Rejected: " + e.getMessage());
}
}
}
class Product {
final String name;
final int priceKyat;
int stock;
Product(String name, int priceKyat, int stock) {
if (priceKyat <= 0) {
throw new IllegalArgumentException("price must be positive");
}
if (stock < 0) {
throw new IllegalArgumentException("stock cannot be negative");
}
this.name = name;
this.priceKyat = priceKyat;
this.stock = stock;
}
Product(String name, int priceKyat) {
this(name, priceKyat, 0);
}
void describe() {
System.out.println(name + " - " + priceKyat + " MMK, stock " + stock);
}
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Constructor က class နာမည်နဲ့ တူပြီး return type မရှိပါ၊ new လုပ်တိုင်း အလိုအလျောက် run ပါတယ်။
- Field နဲ့ parameter နာမည်တူရင် this.field = param လို့ ရေးပါ။
- Constructor တစ်ခု ရေးလိုက်တာနဲ့ default constructor ပျောက်သွားပါတယ်။
- this(...) နဲ့ chaining လုပ်ပြီး validation ကို တစ်နေရာတည်းမှာ ထားကာ မမှန်တဲ့ object ကို မမွေးစေပါနဲ့။