Main Method & Constructors | in Java

 


Main Method 

ජාවා වල අපි මුලින්ම ඉගෙන ගන්න Method එක තමයි මේක.මේ ඇතුලෙ තමයි අපි Method කෝල් කරන්නෙ object හදන්නෙ කොටින්ම කිව්වොත් අපි ගහන කෝඩ් එක Execute කරන්නෙ මේකෙ ඇතුලෙ තමයි.




          public void main(String[] args){
             System.out.println("Hello");
          }



Constructor

Constructor එකක් කියන්නේ method එකක්. හැබැයි මේකෙන් වෙන වැඩේ වෙන්නේ class එකක් පාවිච්චි කරලා object එකක් හදනකොට.Class එකක් ඇතුලේ තමයි Constructor එකක් හදන්නේ.අපි constructors භාවිතා කරන්නේ.අපේ code එක එහෙමත් නැත්නම් software එක run වෙන්න පටන් ගද්දිම දෙන්න ඕනි commands ටික ලබාදෙන්න. පහළ තියෙන Example code එක බලන්න.

class Car{
Car() {
// constructor body
}
}

මේක සාමාන්‍ය method එකක් නෙමෙයි නිසා Constructor එකක් හදනකොට එකට rules තියෙනවා.

  1. Constructor එකේ නම Class එකේ නමට සමාන වෙන්න ඕනේ
  2. Constructor එකට return type එකක් නැ.


අපි Class එකක් හදනකොට Constructor එකක් හැදුවේ නැතත් Java compiler එකෙන් automatically ම code එක run වෙද්දිම හදනවා.



    Comments

    Popular posts from this blog

    Object Oriented Programming (OOP) | In Sinhala

    Understanding Inheritance in Java