Thuta Learning
Java
IntermediateProgrammingအခြေခံ

Modifiers

Javaသင်ခန်းစာ 1825 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Access level လေးဆင့်ကို အကျယ်ဆုံးကနေ အကျဉ်းဆုံးအထိ စီပြီး ဘယ်ကနေ မြင်ရလဲ ရှင်းပြနိုင်ဖို့
  • Field တွေကို private ထားပြီး public method ကနေပဲ ပြောင်းခွင့်ပေးနိုင်ဖို့
  • static field/method က object အားလုံး မျှသုံးတဲ့ class ပိုင် member ဖြစ်တာကို အသုံးချနိုင်ဖို့
  • final variable, method, class တစ်ခုစီရဲ့ အဓိပ္ပာယ်နဲ့ final reference က object ကို immutable မဖြစ်စေတာကို ရှင်းပြနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

ဘဏ်တစ်ခုကို စဉ်းစားကြည့်ပါ။ ငွေသွင်းကောင်တာက လူတိုင်းအတွက် ဖွင့်ထားပါတယ် (public)။ ဝန်ထမ်းအခန်းကို ဒီဘဏ်ခွဲက ဝန်ထမ်းတွေပဲ ဝင်လို့ရပါတယ် (package-private)။ မော်တော်ယာဉ်ထဲက ငွေသေတ္တာကိုတော့ သတ်မှတ်ထားတဲ့ လူတစ်ယောက်တည်းပဲ ဖွင့်ခွင့်ရှိပါတယ် (private)။ Customer က ငွေသေတ္တာထဲ တိုက်ရိုက် လက်နှိုက်လို့ မရဘဲ ကောင်တာကနေပဲ ငွေသွင်း/ထုတ် လုပ်ရပါတယ်။

Java ရဲ့ access modifier တွေက code အတွက် ဒီလို တံခါးတွေပါပဲ။ Non-access modifier တွေကတော့ ဘယ်သူ မြင်ရလဲ မဟုတ်ဘဲ member ရဲ့ သဘောသဘာဝကို ပြောပါတယ် — static က "ဘဏ်တစ်ခုလုံးမှာ တစ်ခုတည်း" (ဘဏ်နာမည်လို)၊ final က "တစ်ခါ သတ်မှတ်ပြီးရင် မပြောင်းရ" လို့ ဆိုလိုပါတယ်။

Problem → Why → Solution

ပြဿနာ: အပြင်ကနေ balance ကို တိုက်ရိုက် ပြင်တာ

Account class မှာ deposit() က အနုတ် ပမာဏကို ငြင်းပေမယ့် balance field ကိုယ်တိုင်က ဘယ်သူမဆို ပြင်လို့ရနေပါတယ်။ Report code ရေးတဲ့ developer တစ်ယောက်က "ခဏ စမ်းကြည့်ရုံ" ဆိုပြီး account.balance = -500; လို့ ရေးလိုက်တာနဲ့ deposit() ရဲ့ စစ်ဆေးမှု အားလုံး အလကား ဖြစ်သွားပါတယ်။ Compiler ကလည်း လက်ခံပါတယ်။

Main.java
public class Main {
  public static void main(String[] args) {
    Account account = new Account();
    account.deposit(-100);    // rejected by the rule
    account.balance = -500;   // bypasses the rule completely
    System.out.println(account.balance); // -500
  }
}

class Account {
  int balance;                // no modifier: visible to the whole package

  void deposit(int amount) {
    if (amount > 0) balance += amount;
  }
}

ဘာကြောင့် private က ဖြေရှင်းပေးလဲ

Field ကို private int balance; လို့ ပြောင်းလိုက်ရင် Account class အပြင်က code မှန်သမျှ balance ကို မမြင်ရတော့ပါဘူး၊ account.balance = -500; က "balance has private access in Account" compile error ဖြစ်သွားပါတယ်။ Balance ပြောင်းဖို့ လမ်းတစ်ခုတည်းက deposit() ဖြစ်လာလို့ rule ကို ကျော်ဖို့ နည်းလမ်း မရှိတော့ပါဘူး။

Core concept: access level လေးဆင့်နဲ့ static, final

Access modifier တွေက class တစ်ခုရဲ့ API နယ်နိမိတ်ကို ဆွဲပါတယ်။ Java မှာ အဆင့်လေးဆင့် ရှိပြီး keyword မပါတဲ့ package-private က default ပါ။ protected က နာမည်ကြောင့် package-private ထက် ကျဉ်းတယ်လို့ ထင်ရပေမယ့် တကယ်တော့ ပိုကျယ်ပါတယ် — package တူ class အားလုံးအပြင် တခြား package ထဲက subclass တွေကိုပါ ခွင့်ပြုလို့ပါ။ Top-level class တစ်ခုကိုတော့ public ဒါမှမဟုတ် package-private ပဲ ပေးလို့ရပြီး file တစ်ခုမှာ public top-level class တစ်ခုပဲ ရှိရကာ file နာမည်နဲ့ တူရပါမယ်။

Modifierဘယ်ကနေ မြင်ရလဲ
publicနေရာတိုင်း
protectedPackage တူ class တွေ + တခြား package က subclass တွေ
(keyword မပါ) package-privatePackage တူ class တွေပဲ
privateကြေညာထားတဲ့ class ထဲမှာပဲ

static ဆိုတာ member က object တစ်ခုချင်းမှာ မဟုတ်ဘဲ class ကိုယ်တိုင်မှာ တစ်ခုတည်း ရှိတယ်လို့ ဆိုလိုပါတယ်။ static int openedCount ဆိုရင် Account object ဘယ်နှခု ဆောက်ဆောက် counter တစ်ခုတည်းကို မျှသုံးပါတယ်။ static method ကို Account.opened() လို class နာမည်နဲ့ ခေါ်ပြီး ခေါ်တဲ့ object မရှိလို့ instance field တွေကို this မပါဘဲ တိုက်ရိုက် သုံးလို့ မရပါဘူး။

final ကတော့ နေရာအလိုက် အဓိပ္ပာယ် သုံးမျိုး ရှိပါတယ်။ final variable/field ကို တစ်ခါပဲ assign လုပ်လို့ရပါတယ်။ final method ကို subclass က override မလုပ်နိုင်ပါဘူး။ final class (String လို) ကို extends မလုပ်နိုင်ပါဘူး။ static final နှစ်ခုတွဲရင် BANK_NAME လို constant ဖြစ်ပြီး နာမည်ကို UPPER_SNAKE_CASE နဲ့ ရေးလေ့ရှိပါတယ်။ abstract ကို Abstraction lesson မှာ၊ sealed, synchronized, volatile လို modifier တွေကို နောက်ပိုင်း သက်ဆိုင်ရာ lesson တွေမှာ တွေ့ရပါမယ်။

ဒီ lesson ရဲ့ example တွေကို java Main.java နဲ့ run တဲ့အခါ file တစ်ခုထဲက class အားလုံးက package တစ်ခုတည်း (unnamed package) ထဲမှာ ရှိပါတယ်။ ဒါကြောင့် Main ကနေ Account ရဲ့ package-private နဲ့ protected member တွေကို မြင်ရပြီး private member တွေကိုပဲ မမြင်ရပါဘူး။ protected နဲ့ package-private ကွာခြားချက်ကို တကယ် မြင်ချင်ရင် package နှစ်ခုနဲ့ project တစ်ခု လိုပါတယ်၊ ဒါကို Inheritance lesson မှာ ပြန်ကြည့်ပါမယ်။

မှတ်ထားရမယ့် ပုံစံ: မေးခွန်း သုံးခု
  1. ဘယ်သူ မြင်ရမလဲ? → access modifier
  2. Object ပိုင်လား class ပိုင်လား? → static
  3. ပြောင်းခွင့် ရှိလား? → final

သံသယရှိရင် အကျဉ်းဆုံး access (private) ကနေ စပြီး တကယ်လိုမှ ချဲ့ပါ။

အထွေထွေ ပုံစံ

java
class ClassName {
  public static final Type CONSTANT = value; // shared, never changes
  private static Type sharedCounter;         // shared, hidden
  private final Type id;                     // per object, set once
  private Type state;                        // per object, hidden

  public ReturnType action(Type arg) { ... } // the public API
  static ReturnType helper() { ... }         // called as ClassName.helper()
  private void internalStep() { ... }        // implementation detail
}

Modifier အစဉ်က access → static → final → type ဖြစ်လေ့ ရှိပါတယ်။ Field တွေကို private ထား၊ အပြင်က သုံးရမယ့် method တွေကိုပဲ public ပေး၊ class အလုံး မျှသုံးရမယ့် အရာတွေကို static ထားပါ။

Example: private state နဲ့ static counter ပါတဲ့ Account

ဒီ example က modifier အမျိုးအစား လေးမျိုးကို class တစ်ခုထဲမှာ ပြပါတယ်: static final constant, private static counter, private final field နဲ့ private field ကို ထိန်းတဲ့ public method တွေပါ။ Comment ပိတ်ထားတဲ့ line ကို ဖွင့်ကြည့်ရင် private access error ကို ကိုယ်တိုင် မြင်ရပါမယ်။ mya.deposit(-300) ရဲ့ ရလဒ်နဲ့ နောက်ဆုံးမှာ static counter က account ဘယ်နှခု ပြမလဲဆိုတာကိုလည်း ခန့်မှန်းကြည့်ပါ။

Main.java
public class Main {
  public static void main(String[] args) {
    System.out.println(Account.BANK_NAME);

    Account aung = new Account("Aung", 5000);
    Account mya = new Account("Mya", 1200);
    aung.deposit(2500);
    mya.deposit(-300); // rejected inside deposit()
    // aung.balance = 1_000_000; // compile error: balance has private access

    System.out.println(aung.owner() + ": " + aung.balance());
    System.out.println(mya.owner() + ": " + mya.balance());
    System.out.println("Accounts opened: " + Account.opened());
  }
}

class Account {
  static final String BANK_NAME = "Demo Bank";
  private static int openedCount = 0;

  private final String owner;
  private int balance;

  Account(String owner, int balance) {
    this.owner = owner;
    this.balance = balance;
    openedCount++;
  }

  public void deposit(int amount) {
    if (amount > 0) {
      balance += amount;
    }
  }

  public String owner() { return owner; }
  public int balance() { return balance; }
  static int opened() { return openedCount; }
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Class-level constantstatic final String BANK_NAME = "Demo Bank";

    static ဖြစ်လို့ object မဆောက်ဘဲ Account.BANK_NAME နဲ့ ဖတ်လို့ရပြီး final ဖြစ်လို့ ဘယ်သူမှ ပြန်ပြောင်းလို့ မရပါဘူး။

  2. Shared counterprivate static int openedCount = 0;

    Account object အားလုံးက counter တစ်ခုတည်းကို မျှသုံးလို့ constructor ထဲက openedCount++ က account ဖွင့်တိုင်း တိုးပြီး နောက်ဆုံး 2 ရပါတယ်။ private ဖြစ်လို့ အပြင်ကနေ ပြင်လို့ မရပါဘူး။

  3. Per-object, set onceprivate final String owner;

    Object တစ်ခုစီမှာ ကိုယ်ပိုင် owner ရှိပြီး constructor မှာ တစ်ခါ assign လုပ်ပြီးနောက် မပြောင်းနိုင်ပါဘူး။ အပြင်က owner() method ကနေပဲ ဖတ်လို့ရပါတယ်။

  4. The only door to balancepublic void deposit(int amount) {

    balance က private ဖြစ်လို့ ဒီ method က ပြောင်းဖို့ တစ်ခုတည်းသော လမ်းပါ။ mya.deposit(-300) ကို if (amount > 0) က ငြင်းလို့ Mya ရဲ့ balance က 1200 အတိုင်း ကျန်ပါတယ်။

  5. Static method callSystem.out.println("Accounts opened: " + Account.opened());

    opened() က static ဖြစ်လို့ object မလိုဘဲ class နာမည်နဲ့ ခေါ်ပါတယ်။ ဒီ method ထဲမှာ balance လို instance field ကို သုံးဖို့ ကြိုးစားရင် compile error ဖြစ်ပါမယ်။

You should see
Demo Bank
Aung: 7500
Mya: 1200
Accounts opened: 2

final reference ≠ immutable object

final က variable ထဲက reference ကို ချုပ်ထားတာပါ၊ reference ညွှန်နေတဲ့ object ကို မဟုတ်ပါဘူး။ final int[] seats ဆိုရင် seats ကို array အသစ် ညွှန်အောင် ပြန် assign လုပ်လို့ မရပေမယ့် seats[0] = 42 လို array ထဲကို ပြင်တာကတော့ လုံးဝ ရပါတယ်။ final List<String> မှာလည်း add() နဲ့ remove() ဆက်ခေါ်လို့ ရပါတယ်။ Object ကိုယ်တိုင် မပြောင်းနိုင်စေချင်ရင် List.of() လို unmodifiable collection ဒါမှမဟုတ် record လို immutable type ကို သုံးရပါတယ်။

Main.java
import java.util.Arrays;

public class Main {
  static final int MAX_SEATS = 3;

  public static void main(String[] args) {
    final int[] seats = new int[MAX_SEATS];
    seats[0] = 42; // allowed: the array object itself is still mutable
    // seats = new int[5]; // compile error: cannot assign a value to final variable
    System.out.println(Arrays.toString(seats));
  }
}

Output က [42, 0, 0] ပါ။ Comment ပိတ်ထားတဲ့ line ကို ဖွင့်လိုက်ရင် "cannot assign a value to final variable seats" error ရပါမယ်။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Production code မှာ API key, password hash, internal cache စတဲ့ field တွေကို private ထားပြီး public method အနည်းငယ်ကနေပဲ ထိန်းချုပ်ပါတယ်။ Library ရေးတဲ့ team တွေအတွက် public ဆိုတာ ကတိတစ်ခုပါ — public method တစ်ခုကို user တွေ သုံးပြီးသွားရင် နာမည်ပြောင်းတာ၊ ဖျက်တာက သူတို့ code ကို ချိုးဖျက်ပါတယ်။ private method ကိုတော့ ဘယ်အချိန်မဆို လွတ်လွတ်လပ်လပ် refactor လုပ်လို့ရပါတယ်၊ ဒါကြောင့် public surface ကို တတ်နိုင်သမျှ သေးအောင် ထားကြပါတယ်။

static final constant တွေက config value, error message, limit တွေအတွက် နေရာတိုင်းမှာ တွေ့ရပါတယ် (MAX_LOGIN_ATTEMPTS = 5 လို)။ ဒါပေမယ့် ပြောင်းလို့ရတဲ့ static field တွေကိုတော့ သတိထားသုံးကြပါတယ် — web server မှာ request အများကြီး တစ်ပြိုင်နက် run နေတဲ့အခါ static counter တစ်ခုကို thread အများက ပြင်ရင် data race ဖြစ်နိုင်ပြီး test တွေကြားမှာလည်း state ကျန်နေတတ်ပါတယ်။ Spring လို framework တွေမှာ dependency တွေကို private final field ထဲ constructor ကနေ ထည့်တာက standard ဖြစ်ပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. final က object ကို immutable လုပ်တယ်ထင်တာ

private final List<String> tags; ရှိပြီး getTags() က list ကို တိုက်ရိုက် ပြန်ပေးရင် caller က getTags().clear() လို့ ခေါ်ပြီး ထဲက data အကုန် ဖျက်လို့ရပါတယ်။ final က reference ကိုပဲ ကာကွယ်ပါတယ်။ List.copyOf(tags) ဒါမှမဟုတ် unmodifiable view ကို ပြန်ပေးပါ။

2. Object တစ်ခုချင်း ပိုင်ရမယ့် data ကို static လုပ်မိတာ

static int balance; လို့ ရေးမိရင် account အားလုံးက balance တစ်ခုတည်းကို မျှသုံးလို့ Aung ငွေသွင်းရင် Mya ရဲ့ balance ပါ တက်သွားပါတယ်။ Object တစ်ခုစီမှာ မတူရမယ့် data မှန်သမျှ static မပါရပါဘူး။

3. အကုန် public ပေးထားတာ

"နောက်မှ လိုရင်" ဆိုပြီး field နဲ့ helper method တွေကို public ပေးထားရင် အပြင်က code တွေက အဲဒါတွေကို မှီခိုလာပြီး နောက်ပိုင်း ပြင်ရခက်ပါတယ်။ private ကနေ စပြီး တကယ်လိုမှ ချဲ့ပါ။ protected ကလည်း package-private ထက် ကျယ်တာကို မမေ့ပါနဲ့။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Field အသစ်တိုင်းကို private နဲ့ စပါ၊ ပြောင်းစရာ မလိုရင် final ပါ ထပ်ထည့်ပါ။
  • ပြောင်းလို့ရတဲ့ static field ကို ရှောင်ပါ၊ static ကို constant (static final) နဲ့ state မရှိတဲ့ helper method တွေအတွက် ဦးစားပေး သုံးပါ။
  • Public method တစ်ခု ထည့်တိုင်း "ဒါကို နှစ်ပေါင်းများစွာ ထိန်းသိမ်းနိုင်မလား" လို့ မေးကြည့်ပါ။

Mini challenge

Mini challenge: နံပါတ်စဉ် ticket များ

Ticket တစ်ခုချင်းကို အလိုအလျောက် နံပါတ်စဉ် တိုးပေးတဲ့ Ticket class ရေးပါ။ အပြင်က code က နံပါတ်ကို ပြင်လို့ မရရပါဘူး။

  • private static int nextNumber = 1 နဲ့ private static final String PREFIX = "TKT-" သုံးရမယ်
  • private final int number နဲ့ private final String event — constructor မှာ တစ်ခါပဲ သတ်မှတ်ရမယ်
  • public String label() က "TKT-1 (Concert)" ပုံစံ ပြန်ရမယ်
  • static int issued() က ထုတ်ပြီးသား ticket အရေအတွက်ကို ပြန်ရမယ်
Main.java
public class Main {
  public static void main(String[] args) {
    // TODO: issue two "Concert" tickets and one "Football" ticket
    // TODO: print each label(), then "Issued: " + Ticket.issued()
  }
}

class Ticket {
  // TODO: a shared counter starting at 1 and a constant prefix "TKT-"

  // TODO: per-ticket number and event that can never change

  Ticket(String event) {
    // TODO: store the event, take the next number, advance the counter
  }

  // TODO: public label() -> "TKT-1 (Concert)"

  // TODO: static issued() -> number of tickets created so far
}

ရလဒ်: ဒီလို လေးကြောင်း ထွက်ရပါမယ်: TKT-1 (Concert) TKT-2 (Concert) TKT-3 (Football) Issued: 3

အရိပ်အမြွက် ကြည့်ရန်

Constructor ထဲမှာ this.number = nextNumber; ပြီးမှ nextNumber++; လုပ်ပါ။ issued() က nextNumber - 1 ကို ပြန်ပေးရုံပါပဲ၊ ဒါမှမဟုတ် သီးခြား static counter တစ်ခု ထားလည်း ရပါတယ်။

အဖြေ ကြည့်ရန်
Main.java
public class Main {
  public static void main(String[] args) {
    Ticket first = new Ticket("Concert");
    Ticket second = new Ticket("Concert");
    Ticket third = new Ticket("Football");

    System.out.println(first.label());
    System.out.println(second.label());
    System.out.println(third.label());
    System.out.println("Issued: " + Ticket.issued());
  }
}

class Ticket {
  private static int nextNumber = 1;
  private static final String PREFIX = "TKT-";

  private final int number;
  private final String event;

  Ticket(String event) {
    this.event = event;
    this.number = nextNumber;
    nextNumber++;
  }

  public String label() {
    return PREFIX + number + " (" + event + ")";
  }

  static int issued() {
    return nextNumber - 1;
  }
}

Quick Quiz 1 / 3

Package-private (keyword မပါ) နဲ့ protected ကို နှိုင်းယှဉ်ရင် ဘယ်ဟာ မှန်လဲ။

Quick Quiz 2 / 3

final int[] data = {1, 2}; ပြီးရင် ဘယ် statement က compile error ဖြစ်မလဲ။

Quick Quiz 3 / 3

class Counter { static int count; Counter() { count++; } } ကနေ object သုံးခု ဆောက်ပြီးရင် Counter.count က ဘယ်လောက်လဲ။

မှတ်ထားရမယ့် အချက်များ

  • Access level: public > protected > package-private (keyword မပါ) > private၊ protected က package-private ထက် ကျယ်ပါတယ်။
  • Field တွေကို private ထားပြီး rule စစ်တဲ့ public method တွေကနေပဲ ပြောင်းခွင့်ပေးပါ။
  • static member က class ပိုင်ဖြစ်ပြီး object အားလုံး မျှသုံးပါတယ်၊ static method ထဲမှာ this မရှိပါဘူး။
  • final က variable ကို တစ်ခါ assign၊ method ကို override မရ၊ class ကို extend မရအောင် လုပ်ပြီး reference ညွှန်တဲ့ object ကိုတော့ immutable မလုပ်ပါဘူး။

Java Language Specification, Java SE 25 — Oracle

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Modifiers | Thuta Learning