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);
}
}
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 කරලා බලන්න.
ස්තූතියි….