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 වලින් හම්බෙමු එහෙනම්. ස්තූතියි….  

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