මොනවද මේ 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).
- 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 වලින්
හම්බෙමු එහෙනම්. ස්තූතියි….
No comments:
Post a Comment