class variable and instance variable in Java

Class variables (Static variable)

Class variables are variables declared within a class, outside any method, with the static keyword.

Static key word එක භාවිතා කරලා, class එකක් ඇතුලේ, method එකකින් පිටතදී declare හා initialize කරන  variables වලට අපි කියනවා class variables එහෙමත් නැත්නම් satatic variables කියලා. Static key word එක use කරලා variable එකක හැදුවොත් ඒක ඒ class එක ඇතුලේ තියන සියලුම method වලට access කරන්න පුලුවන්. ඒ වගේම  variable එකේ අගයත් වෙනස් කරන්න පුලුවන් එනවා. අපි මේකටත් example program එකක් බලමු. 

public class StaticVariableDemo {
    public static String myClassVar="this is static variabale";
    public static void main(String args[]){
      StaticVariableDemo obj = new StaticVariableDemo();
      StaticVariableDemo obj2 = new StaticVariableDemo();
                 //All three will display "class or static variable"
      System.out.println(obj.myClassVar);
      System.out.println(obj2.myClassVar);
                  //changing the value of static variable using obj2
      obj.myClassVar = "Changed Value";
                  //All three will display "Changed Text"
      System.out.println(obj.myClassVar);
      System.out.println(obj2.myClassVar);  
   } 
}
 output

හරි අපි බලමු මේ program එකත් මොකද මේකෙන් වෙන්නේ කියලා.

public static String myClassVar="this is static variabale";

මේ line එකේ තියන myClassVar word එක තමා class variable එකේ එහෙමත් නැත්නම් static variable එකේ name එක වෙන්නේ. Main method එක ඇතුලේ මොකද කරලා තියෙන්නෙ කියලා බලමු අපි. StaticVariableDemo කියන class එකෙන් object දෙකක් හදලා තියනවා “obj” සහ “obj2” කියන නම්වලින්. ඊට පස්සේ කරලා තියෙන්නේ අර class variable එක object දෙකම භාවිතා කරලා print කරගන්නවා. Out put එක විදිහට "this is static variabale" දෙපාරක්  print  වෙනවා.

      obj.myClassVar = "Changed Value";

මේ line එකෙන් කරලා තියෙන්නේ, පලවෙනි object name එක භාවිතා කරලා static variable එකට “change value” කියන text එක assign කරන එක. ඊට පස්සෙත් කලින් වගේම object දෙක භවිතාකරලා class variable එක print කරගන්නවා. එතකොට output එක විදිහට ලැබෙන්නේ Changed Value කියන text එකම දෙපාරක් print වෙලා.

මේකට හේතුව තම මෙ variable එක static key word එක සමග declare කර තිබීම. මේ class එක ඇතුලේ වගෙම වෙනත් class තුලත් access කරන්න පුලුවන් හැබැයි මේ static variable එක තියන class එක extend කරන්න ඕනි.    

Instance variable (Non-static variable)

Instance variables are variables within a class but outside any method.

මේ variable type එකත් උඩින්කිව්ව එක වගේමයි හැබැයි මෙතන static key word එකක් නෑ. Declare කරන්නෙත් initialize කරන්නෙත් methods වලින් පිට හැබැයි class එකක් ඇතුලේ. මේ variables class එකේ ඕනිම method එකක් ඇතුලෙ access කරන්න පුලුවන්. මේ instance variable වල විශේෂ ලක්ෂණයක් තමයි, අපි variable එක වෙන තැනකදී access කරනකොට කලින් එකෙ copy එකක් තමා access වෙන්නේ. අපි sample program එකකුත් එක්ක බලමු එතකොට හොදටම තෙරෙයි.

public class InstanceVariable {
    String myInstanceVar = "instance variable";
    public static void main(String args[]) {
       InstanceVariable object1 = new InstanceVariable();
       InstanceVariable object2 = new InstanceVariable();
        System.out.println(object1.myInstanceVar);
        System.out.println(object2.myInstanceVar);
        object1.myInstanceVar = "java programmer";
        System.out.println(object1.myInstanceVar);
        System.out.println(object2.myInstanceVar);
    }
}

output

String myInstanceVar = "instance variable";
මේකත් අර විදිහමයි හැබැයි static key word එක නෑ. මෙකෙත් object දෙකක් තියනවා. Object1, object2 කියලා. පලවෙනි පාර object දෙක භාවිතා කරලා myInstanceVar කියල variable එක print කරනකොට print වෙන්නෙ "instance variable" කියලා.  

        object1.myInstanceVar = "java programmer";

මේ line එකේදී object1 එකෙන් variable value එක java programmer කියල change කරනවා. ඊට පස්සෙත් උඩින් වගේම object දෙක use කරලා myInstanceVar කියන variable එක print කරනවා. මෙතනදී, object1 එකට අදාලව print line එකේ output එක වෙන්නේ “java programmer” කියන එක. Object2 එකට අදාල print line එකේ output එක වෙන්නේ කලින් එකමයි ඒ කියන්නෙ instance variable කියන එක.  Class variable එකේදී මේ variable දෙකම වෙනස් වුනා ඒත් මේකේ එහෙම නෑ, ඒක තමා මේ variable type දෙක අතරේ තියන ප්‍රධානම වෙනස. මේ ගෙන මම ගොඩක් කියන්න යන්නෙ නෑ මොකද local variable ගෙන කියනකොටත් අපි මේ ගෙන කතා කරා. හරි එහෙනම් දැනට නවතිනවා මේ programs අරගෙන compile කරලා බලන්න.

ස්තූතියි….     

මොනවද මේ variable කියන්නේ හා ඒවගේ කොටස් මොනවද ?


මේකනම් ගොඩක් වැදගත් ලිපියක්. එහෙමයි කියලා අනිත් ඒවා වැඩක් නෑ කියනවා නෙවෙයි. මේ ලිපියෙන් කියන්නෙ java වල programming part වලට ගොඩක්ම වැදගත් වෙන තොරතුරු ටිකක්. Normally කියනවනම් variable එකක් කියන්නේ කුමක් හෝ data type එකක data එකක් store කරගන්න ram එකේ හදගන්න space එකක්. අපි බලමු variable එකක් ලියන්නෙ කොහොමද කියලා.

Variable declaration
int i;

initializing variable

i = 10; 

variable එකක් ලිවීමෙදී අපි මේ methods අනුගමනය කලයුතුයි. Variable declaration කියන එකෙන් කරන්නෙ variable එකේ data type එකයි, variable name එකයි දෙන එක. Variable initialize කිරීමේදී අපි කරන්නේ declare කරගත්ත variable එකට අගයක් assign කරන එක. මේ වැඩ දෙකම අපිට එක් line එකකදී කරන්න පුලුවන් මේ විදිහට.
 int i = 10;

හරි අපි දැන් බලමු මොනවද java වල තියන variable types.  ප්‍රධාන වශයෙන් කොටස් තුනක් තියනවා.
-        
     Local variable.
-  Class variable (static variable).
-  Instance variable (Non-static variable). 

Local variable

Variables defined inside methods, constructors or blocks are called local variables.

Local variable කියන්නේ ඇත්තටම class එකක method එකක් හෝ constructor එකක් ඇතුලේ declare හා  initialize කරන variables වලට. ඒ variable එක ඕනේම data type එකකින් යුක්ත විය හැකියි. ඒ variables method එකෙන් බාහිරව access කිරීමට හැකියාවක් නෑ. ඒ වගේම තමා ඒ method එකෙන් පිටතදී මේ type එකේ variables වල අගයන් වෙනස් කරන්නත් බෑ. අපි උදාහරණයක් අරගෙනම බලමු.

public class LocalVariableText {  
   public String myVar="instance variable";
         public void myMethod(){
       // local variable
       String myVar = "Inside Method, local variable";
       System.out.println(myVar);
   }
    public static void main (String args[]){  
     // Creating object
      LocalVariableText obj = new LocalVariableText();     
      obj.myMethod();          
      System.out.println(obj.myVar);
    }
}

හරි අපි දැන් අපි බලමු මේ code එකෙන් මොකද වෙන්නේ කියලා. මේ program එකේ තියන class එකේ name එක LocalVariableText කියන එක , method name එක වෙන්නේ myMethod කියන එක. ඊට අමතරව main method එකත් තියනවා.

public String myVar = "instance variable";

මේ line එකේ තියෙන්නේ  string type එකේ instance variable එකක්. ඒ variable එක භාවිතා කරලා තියෙන්නෙ program එක පැහැදිලිවෙන්න. හරි දැන් අපි බලමු main method එක ඇතුලේ මොකද වෙන්නේ කියලා.  Main method එක ගැන කියන්න දෙයක් නැනේ. එකත් අව්ල් වගේනම් blog එකේ තියනවා බලාගන්න පුලුවන්. පලවෙනියටම අපි කරලා තියෙන්නේ class එකෙන් object එකක් හදගන්න එක. Object name එක තමයි “obj” කියන්නේ. එතනට ඔයාට ඕනේ name එකක් දාගන්න පුලුවන්. ඊලගට කරලා තියෙන්නේ object එක මගින් method එකට call කරනවා. දැන් myMethod() එකෙ body එක බලන්න. එකේ තියනවා string type එකේ myVar කියන variable එක. "Inside Method, local variable" කියන text එක ඒ variable එකට assign කරලා තියනවා.   

String myVar = "Inside Method, local variable";

මෙතන තියන variable එක තමා local variable එක වෙන්නේ. ඒකට හේතුව තමා මේ method එක තියෙන්නේ myMethod කියන method එක ඇතුලේ නිසා. මේ local variable එකේ scope එක එහෙමත් නැත්නම් access කලහැකි පරාසය වෙන්නේ මේ method එක ඇතුලත විතරයි.   
       System.out.println(myVar);

  මේ විදියට ඊලගට අපි local variable එක print කරගන්නවා. Output එක වෙන්නේ Inside Method, local variable කියන text එක. මොකද myVar variable එකේ value එක ඒක නිසා. ඊලගට instance variable කියලත් මේ program එකේදි print වෙනවා. හොදට main method එක බලන්න්න.

      System.out.println(obj.myVar);

මේ line එකත් main method එකේ අවසානෙට තියනවා. එකත් class object එක මගින් myVar කියන variable එක තමා access කරලා තියෙන්නේ. හොදට බලන්න myMethod() එක ඇතුලේ තියන string variable එකත් ඒකට උඩින්, method එකෙන් පිට තියන string variable එකත් එකම name එකෙන් තමා තියෙන්නේ.  

System.out.println(obj.myVar) 

main method එකෙ අවසානෙට තියන මේ line එකේ myVar variable එකට assign කරලා තියෙන්නේ “instance variable කියන text එක. ඒ නිසා  value එක විදිහට print වෙන්නේ “instance variable” කියලා. ඒකට හේතුව තමා local variable එකේ scope එක method එක ඇතුලට විතරක් සීමා වෙන නිසා. අනිත් හෙතුව තමා instance variable එකේ scope එක සම්පූර්ණ class  එක පුරාවටම තියන නිසා. මේ හේතු හින්දා තමා එකම නමින් variable දෙකම තිබ්බත් print වෙන අගයන් වෙනස් වෙන්නේ. මෙතනදී print වෙන්නේ local variable  එක නෙවෙයි.  Program එක අරගෙන run කරලා බලන්න.

output 



Class variable හා instance variable වලින් හම්බෙමු එහෙනම්. ස්තූතියි….  

operators (operator part 3)

Relational operators

අද කතකාරන්න යන්නේ relational හා ternary operators ගෙන. මේ relational operators වල ප්‍රධාන කාර්ය වෙන්නේ numbers දෙකක් එහෙමත් නැත්නම් අගයන් දෙකක් සංසන්දනය (compare) කරලා Boolean අගයක් (true or false) return කරන එක. ජාවා වල මේ type එකේ operators හයක් තියනවා.

<    less than                                         ( x < y ,  x කුඩයි y ට වඩා).
>    greater than                                    ( x > y ,  x විශාලයි y ට වඩා).
<= less than or equal                      ( x <= y ,  x කුඩයි හෝ සමානයි y ).
>= greater than or equal                ( x >= y , x විශාලයි හෝ සමානයි y ).
== equal                                         ( x == y , x සමානයි y ).
!=   not equal                                  ( x != y , x අසමානයි y ).


මේ operators ටික නම් බැලූ බැල්මටම ලේසි තේරුම් ගන්න. මේ operators හයටම අපි එක program එකක් බලමු.  ඊට පස්සෙ ටිකක් කතාකරමු.

public static void main(String[] args) {
        int x = 10;
        int y = 15;
        System.out.println("x less than y :" + (x < y));
        System.out.println("x greater than y :" + (x > y));
        System.out.println("x less than or equal to y :" + (x <= y));
        System.out.println("x greater than or equal to y :" + (x >= y));
        System.out.println("x equal to y :" + (x == y));
        System.out.println("x not equal to y :" + (x != y));
    }

ඔයාලට පෙන්නව මේ program එකේ මම x වල අගය විදිහට දීල තියෙන්නේ 10 ඒ වගේම y වල අගය දීල තියෙන්නේ 15. අපි මේක relational operators භාවිතාකරලා කියනවනම් මෙහෙමයි ( x < y ) x කුඩයි y ට වඩා. මේක තමා එතකොට හරි condition එක. මම දීල තියන program එකේ මේ condition එක භාවිතාකරලා තියෙන්නේ පලවෙනි print line එකේ.

“ System.out.println("x less than y :" + (x < y)); “

බැලූ බැල්මට එහෙම පෙනුනට මේ program එකේ condition තුනක් හරි. අපි බලමු ඒ මොනවද කියලා.

1. System.out.println("x less than y :" + (x < y));
2. System.out.println("x not equal to y :" + (x != y));
3. System.out.println("x less than or equal to y :" + (x <= y));


මේ line තුනම හොදට බලන්න. එක, x කුඩයි y ට වඩා. දෙක, x අසමානයි y ට. තුන, x කුඩයි හෝ සමානයි y ට. එහෙනම් මේ condition තුනම හරි.  මෙන්න output එක.


මේ program එක run කරල බලන්න උඩින් කියපු condition තුන තියන lines වලට අදාලව ture කියල එයි ඉතුරු ඔක්කොම lines වලට අදාලව false print වෙයි. අපි තවත් program එකක්  if else use කරලා බලමු තේරුම්ගන්න ලේසි වෙන්න.

public static void main(String[] args) {
        int x = 10;
        int y = 15;

        if (x == y) {
            System.out.println("X equal to Y.");
        } else if (x > y) {
            System.out.println("X greater than Y.");
        } else if (x <= y) {
            System.out.println("X less than or equal to Y");
        } else if (x >= y) {
            System.out.println("X greater than or equal to Y");
        } else if (x != y) {
            System.out.println("X not equal to Y");
        } else if (x < y) {
            System.out.println("X less than Y.");
        }
    }

If else දන්නැත්නම් ඒකට අවුල්ක් ගන්න එපා. මේකෙන් කරන්නේ if , else if එකේ වරහන් ඇතුලේ තියන condition එක check කරලා output එකක් දෙන එක. මේක වැඩකරන්නේ මෙහෙමයි. පලවෙනියටම if එක ඇතුලෙ තියන condition එක check කරනවා. ඒක වැරදිනම් ඊලගට තියන else if එක ඇතුලට ගිහින් එකේ තියන condition එක check කරනවා. එකත් වැරදිනම් ඊලගට තියන else if එකේ condition එක check කරනවා. හැබැයි if එකෙ condition එක හරිනම් ඒක ඇතුලට ගිහින් එකේ තියන "X equal to Y." කියන line එක print කරනවා. එතනින්  ඉවරයි. එක condition එකක් වැරදිනම් විතරයි අනිත් එකට යන්නේ. Output එක මෙන්න.


ඉතින් මේ program එකේ තුන් වෙනියට තියන condition එක බලන්න. ඒක තමා පලවෙනියටම true වෙන condition එක. ඉතින් ඒ line එක print කරලා program එක නතර වෙනවා. Program එක compile කරලා බලන්න. If else ගෙන වැඩිය කතාකරන්නේ නෑ ඒක වෙනම අපි කතා  කරමු.

Ternary Operator

අපි ternary operator විදියට operators දෙකක් හදුනගත්තා පලවෙනි lesson එකේදී. ( ? , : ) මේ operator දෙක තමා java වල ternary operator වෙන්නේ. දැන් අපි බලමු ඒවා කොහොමද භාවිතා වෙන්නේ කියලා. මේ operator දෙකම අපිට if else වෙනුවට භාවිතා කරන්නපුලුවන්. අපි බලමු කොහොමද ඒක වෙන්නේ කියලා. If else වැඩකරනහැටි උඩ විස්තරේදී පොඩ්ඩක් දැන ගත්ත නිසා මේ program එක බලන්න.

public static void main(String[] args) {
        int number = -5;
        if (number > 0) {
            System.out.println(number + " is positive");
        } else {
            System.out.println(number + " is not positive");
        }
}

int variable එකක් තියනවා number කියලා අපි එකට -5 කියන අගය සමාන කරලා තියනවා. If condition එක ඇතුලෙදි number variable එකේ අගය 0 ට වඩා වැඩීනම් ඒක positive අගයක් ලෙසත්, එහෙම නැත්නම් ඒ අගය positive  නොවන අගයක් ලෙසත් print කරන්න කියල තමා මේ program එකෙන් code කරලා තියෙන්නේ.  දැන් අපි බලමු ternary operators භාවිතාකරලා කොහොමද මේ program එක code කරන්නේ කියලා.  

public static void main(String[] args) {
        int number = -5;
  String result;
result = (number > 0) ? "positive" : "not positive";
        System.out.println(number + " is " + result);}

මේ program එකේත් උඩින් එකේ තිබුන int variable එකයි, තව string type එකේ variable එකක් තියනවා result කියන name එකෙන්. මේ String කියන්නේ character values store කරලා තියගන්න පුලුවන් data type එකක් කියල දැනට මතක තියාගන්න.  මේ program එක කියවන්නේ මෙහෙමයි.

number කියන variable එකෙ තියන අගය බිංදුවට වඩා විශාලයිනම් result කියන String variable එකට සමාන කරන්නලු “positive” කියලා. එහෙම නැත්නම් සමාන කරන්නලු “not positive” කියලා. මේ program එක අරගෙන run කරලා බලන්න. If else එක භාවිතා කරල කරපුදේමයි මේකෙනුත් කරල හියෙන්නේ. “?” (question mark) එකෙන් if condition එක ඇතුලෙදි කරන දේත් “:” (colon) එකෙන් else part එකත් සම්පූර්න වෙනවා. “for each” loop එකට එහෙමත් මේ operator එක භාවිතා වෙනවා.

System.out.println(number + " is " + result);

මේ line එකේදී කරලා තියෙන්නේ number කියන variable එකයි, “is” කියන word එකයි, result කියන variable එකයි print කරනඑක. Plus mark (+) එකෙන් කරලා තියෙන්නේ මේ  අගයන්,ඒ වගේම is කියන word එක එකිනෙකට සම්බන්ධ කරනඑක. Variable එකක් print කර ගන්න ඕනිනම් double quotes ("")  භාවිතා නොකර නිකම්ම print කරගන්නවා මේ  විදිහට.

System.out.println(number);

Variable දෙකක් හෝ ඊට වඩා print කරගන්න ඕනිනම් මෙහෙමයි.

System.out.println(number + result); 
// System.out.println(number + result + number);

හැබැයි අපිට වෙන මොකක් හරි වචනයක්, වාක්‍යක් මේ අතරට දාගන්න ඕනිනම් වරහන් භාවිතා කරන්න වෙනවා. මේ වගේ.

System.out.println(number + " is " + result);

ඔන්න ඒ ටිකත් එතනදිම කිව්වා එහෙනම් තවත් පාඩමකින් හම්බෙමු. 
ස්තූතියී.....

class variable and instance variable in Java Class variables (Static variable) Class variables are variables declared within a class,...