DEV Community

Cover image for Java Class and Object
Zuki Desu
Zuki Desu

Posted on • Updated on

Java Class and Object

java က object-oriented language ဖြစ်တာကြောင့် class နဲ့ object တွေက အရေးပါလှပါတယ်။ class ဆိုတာက object တွေတည်ဆောက်လို့ရတဲ့ template တစ်ခုပေ့ါ။ real world မှာဆိုလို့ရှိရင် car အတွက် attributes(color, price, size, etc) နဲ့ methods(drive, upgrade, broke, etc) တွေကို car ဆိုတဲ့ class ထဲမှာ ကြေငြာပြီးတော့ လိုအပ်တဲ့အခါတိုင်း object ကနေတဆင့်ပြန်ခေါ်သုံးလို့ရပါတယ်။ ဆိုတော့ classတစ်ခုကို create လုပ်ကြရအောင်။

 class car{     //creating a class
     String color, brand;   //attributes
     int price;

     public static void carCondition(){     //method
         System.out.println("everything is good");
     }
 }
Enter fullscreen mode Exit fullscreen mode

အပေါ်က code မှာဆိုလို့ရှိရင် car ဆိုတဲ့ class မှာပါဝင်တဲ့ (color, brand, price)attributes သုံးခုနဲ့ (carCondition) method တစ်ခုကို လိုအပ်တဲ့အခါတိုင်း object ကနေ တဆင့် ပြန်ခေါ်သုံးလို့ရမှာပါ

class အကြောင်း အကြမ်းဖျင်း သိသွားကြပြီဆိုတော့ အပေါ်က classကို main class ကနေပြီးတော့ objectအနေနဲ့ ပြန်ခေါ်သုံးကြည့်ရအောင်။

 class car{     //creating a class
     String color, brand;   //attributes
     int price;

     public void carCondition(){     //method
         System.out.println("everything is good");
     }
 }

 public class App {
     public static void main(String[] args) throws Exception {
         car myCar = new car();     //creating an object

         myCar.color = "red";       //insert values to created object
         myCar.brand = "Toyota";
         myCar.price = 10000;

         System.out.println(myCar.color);       //get value from inserted object
         System.out.println(myCar.brand);  
         System.out.println("$ "+myCar.price); 
         myCar.carCondition();      //call a method that we created in class
    }
 }
Enter fullscreen mode Exit fullscreen mode

အပေါ်က code မှာဆိုလို့ရှိရင် ကျနော်တို့ createခဲ့တဲ့ car classကိုပဲ main app classကနေ myCar ဆိုတဲ့ object ကို create လိုက်ပြီး အဲ့ object ကနေပဲ attributes valueတွေထည့်လို့ရမယ် ပြန်ခေါ်သုံးမယ် ပြီးရင် method တွေကို ပြန်ခေါ်သုံးမယ် စတာတွေကို အသုံးပြုခဲ့တာပါ။

အဲ့တာဆို ကျနော်တို့ classမှာ constructor တစ်ခုတည်ဆောက်ကြည့်ရအောင်။ အရင်ဆုံး constructor ကို ဘာလို့သုံး ကြတာလဲပေါ့နော်။ အပေါ်က code မှာဆိုလို့ရှိရင် object create လုပ်ပြီးတာနဲ့ attributes တွေထဲ value တွေကို တစ်ခုချင်းဆီထည့်ခဲ့ကြတယ်။ အဲ့လိုဆို အလုပ်ရှုပ်တာပေါ့နော်။ အဲ့လိုမလုပ်ပဲ object create လုပ်ကတည်းက value တွေကို parameter အနေနဲ့ တခါတည်း ထည့်ပေးလိုက်လို့ရပါတယ်။ ဟုတ်ပြီ ရေးကြည့်ရအောင်။

 class car{     //creating a class
     String color, brand;   //attributes
     int price;

     public car(String newColor,String newBrand,int newPrice){
         color = newColor;
         brand = newBrand;
         price = newPrice;
     }
     public void carCondition(){     //method
         System.out.println("everything is going fine");
     }
 }

 public class App {
     public static void main(String[] args) throws Exception {
         car myCar = new car("red", "Tesla", 100000);     //creating an object with parameter

         System.out.println(myCar.color);       //get value from inserted object
         System.out.println(myCar.brand);  
         System.out.println("$ "+myCar.price);

         myCar.carCondition();      //call a method that we created in class
    }
 }
Enter fullscreen mode Exit fullscreen mode

ကျနော် ပြောသွားတဲ့ထဲမှာ နားမလည်တာရှိရင် ဒါမှမဟုတ် ဖြည့်စွတ် ပြောချင် တာများရှိရင် discussion ထဲမှာ ဝင်ရောက်ရေးသွားလို့ရပါတယ်။ ကျေးဇူးတင်ပါတယ်။

Top comments (4)

Collapse
 
pilawnan profile image
Hnin-Su-Myat

throw Exception က class 2ခု connect လုပ်ရင်ထည့်သုံးပေးရတာလားရှင့်

Collapse
 
zukigit profile image
Zuki Desu

မဟုတ်ပါဘူးဗျာ။ error handling မှာသုံးတာပါ ။ ဒီcode မှာပါနေတာက IDE ကdefault အနေနဲ့ ထည့်ပေးလိုက်လို့ပါ

Collapse
 
hareom284 profile image
Harry

Nice article

Collapse
 
zukigit profile image
Zuki Desu

thank dude