ජාවා වල Class Methods සහ Variables - සම්පූර්ණ මාර්ගෝපදේශය | Java SC Guide

ආයුබෝවන් යාළුවනේ, කොහොමද ඉතින් ඔයාලට? අද අපි කතා කරන්න යන්නේ Java Programming වල ගොඩක් වැදගත්, හැබැයි සමහරුන්ට පොඩ්ඩක් අවුල් වෙන්න පුළුවන් මාතෘකාවක් ගැන. ඒ තමයි Class Methods සහ Variables.
අපි දන්නවනේ Java කියන්නේ Object-Oriented Programming (OOP) භාෂාවක් කියලා. මේකේදී හැමදේම වගේ තියෙන්නේ Objects සහ Classes වටා. Class එකක් කියන්නේ නිකන් blueprint එකක් වගේ. අපි ගෙයක් හදන්න කලින් blueprint එකක් අඳිනවා වගේ, software එකක object එකක් හදන්න කලින් Class එකක් හදාගන්නවා. මේ Classes ඇතුළේ තමයි data (Variables) සහ ඒ data එකත් එක්ක වැඩ කරන්න පුළුවන් functions (Methods) තියෙන්නේ.
ගොඩක් අය Java ඉගෙනගන්නකොට Class Methods සහ Variables ගැන හොඳට තේරුම් ගන්නේ නැතුව ඉස්සරහට යනවා. එතකොට code ලියද්දි මොන වෙලාවට මොන variable එකද, මොන method එකද පාවිච්චි කරන්නේ කියලා පැටලෙනවා. මේකෙන් code එක කියවන්න අමාරු වෙනවා වගේම, පස්සේ maintenance කරන්නත් අමාරු වෙනවා. ඒ නිසා, අද අපි මේක හොඳටම පැහැදිලි කරගමු, ඒ වගේම Sri Lankan accent එකට හරියන්නම කතා කරමු!
අද අපි බලමු:
- Instance Variables සහ Static Variables කියන්නේ මොනවද කියලා.
- Instance Methods සහ Static Methods කියන්නේ මොනවද කියලා.
- මොන වෙලාවට මොනවා පාවිච්චි කරන්න ඕනද කියලා.
- ප්රායෝගික උදාහරණ කිහිපයක්.
Variables - ක්ලාස් එකක දත්ත ගබඩා කරන හැටි
මුලින්ම බලමු මේ Variables කියන්නේ මොනවද කියලා. Variable එකක් කියන්නේ data එකක් store කරගන්න පුට්ටු පෙට්ටියක් වගේ දෙයක්. Class එකක් ඇතුළේ Variables වර්ග දෙකක් තියෙනවා: Instance Variables සහ Static Variables.
Instance Variables (Non-Static Variables)
Instance Variables කියන්නේ object එකකටම ආවේණික Variables. ඒ කියන්නේ, අපි එකම Class එකෙන් Objects කීපයක් හැදුවොත්, හැම Object එකකටම තමන්ටම ආවේණික Instance Variables set එකක් තියෙනවා. මේවා Memory එකේදී වෙන වෙනම space එකක store වෙනවා.
උදාහරණයක් විදියට අපි Student
කියලා Class එකක් හදනවා කියමු. Student කෙනෙක්ට name
, age
, studentId
වගේ දේවල් තියෙනවනේ. මේවා තමයි Instance Variables. Student Object එකක් හැදුවම, ඒ Object එකටම ආවේණික name
, age
, studentId
ටිකක් තියෙනවා. තව Student Object එකක් හැදුවොත්, ඒකටත් වෙනම ඒවා ටිකක් තියෙනවා.
class Student {
String name; // Instance Variable
int age; // Instance Variable
String studentId; // Instance Variable
public Student(String name, int age, String studentId) {
this.name = name;
this.age = age;
this.studentId = studentId;
}
public void displayStudentInfo() {
System.out.println("Name: " + name + ", Age: " + age + ", ID: " + studentId);
}
}
public class MySchool {
public static void main(String[] args) {
Student student1 = new Student("Nimal", 20, "S001");
Student student2 = new Student("Kamala", 22, "S002");
student1.displayStudentInfo(); // Name: Nimal, Age: 20, ID: S001
student2.displayStudentInfo(); // Name: Kamala, Age: 22, ID: S002
}
}
මේ උදාහරණයේදී, student1
සහ student2
කියන Objects දෙකටම වෙන වෙනම name
, age
, studentId
තියෙනවා. ඒ නිසා අපි student1.name
වෙනස් කළොත්, ඒකෙන් student2.name
එකට කිසිම බලපෑමක් වෙන්නේ නෑ.
Static Variables (Class Variables)
Static Variables කියන්නේ Class එකටම අයිති Variables. මේවා Objects වලට ආවේණික නෑ. ඒ කියන්නේ, Class එකෙන් Object කීයක් හැදුවත්, Static Variable එක තියෙන්නේ එකයි, ඒක හැම Object එකකින්ම Share වෙනවා. මේවා Class එක load වෙනකොටම Memory එකට load වෙනවා.
උදාහරණයක් විදියට, අපේ Student
Class එකේ, schoolName
කියලා Variable එකක් තියෙනවා කියමු. මේක හැම Student කෙනෙක්ටම පොදුයිනේ, මොකද හැමෝම එකම ඉස්කෝලේනේ. එතකොට මේක Static Variable එකක් විදියට ගන්න පුළුවන්. ඒ වගේම, totalStudents
වගේ ගණනක් තියාගන්නත් Static Variable එකක් පාවිච්චි කරන්න පුළුවන්.
class Student {
String name;
int age;
String studentId;
static String schoolName = "Isipathana College"; // Static Variable
static int totalStudents = 0; // Static Variable
public Student(String name, int age, String studentId) {
this.name = name;
this.age = age;
this.studentId = studentId;
totalStudents++; // Every time a new student is created, increment this.
}
public void displayStudentInfo() {
System.out.println("Name: " + name + ", Age: " + age + ", ID: " + studentId + ", School: " + schoolName);
}
}
public class MySchool {
public static void main(String[] args) {
Student student1 = new Student("Amal", 21, "S003");
student1.displayStudentInfo(); // Name: Amal, Age: 21, ID: S003, School: Isipathana College
Student student2 = new Student("Saman", 23, "S004");
student2.displayStudentInfo(); // Name: Saman, Age: 23, ID: S004, School: Isipathana College
System.out.println("Total Students: " + Student.totalStudents); // Accessing static variable via Class name
Student.schoolName = "Royal College"; // Static variable can be changed via Class name
student1.displayStudentInfo(); // Name: Amal, Age: 21, ID: S003, School: Royal College (changed for all)
}
}
schoolName
සහ totalStudents
කියන Variables දෙක static
කියලා declare කරලා තියෙන නිසා, ඒවා අදාල Class එකේ හැම Object එකකටම පොදුයි. අපි Student.totalStudents
කියලා ගත්තම හැම Student Object එකක්ම හැදෙනකොට ඒක වැඩිවෙනවා, ඒකෙන් හැමෝටම දැනගන්න පුළුවන් මුළු students ගණන කීයද කියලා.
Methods - ක්ලාස් එකක වැඩ කරන හැටි
Variables කියන්නේ Class එකේ දත්ත නම්, Methods කියන්නේ ඒ දත්තත් එක්ක මොනවද කරන්න පුළුවන් කියන එක. ඒ කියන්නේ, Class එකක functions, behaviour එක. Methods වර්ග දෙකක් තියෙනවා: Instance Methods සහ Static Methods.
Instance Methods (Non-Static Methods)
Instance Methods කියන්නේ Object එකකටම ආවේණික Methods. මේවා සාමාන්යයෙන් Instance Variables එක්ක වැඩ කරන්න තමයි පාවිච්චි කරන්නේ. Instance Method එකක් call කරන්න නම්, අදාල Class එකේ Object එකක් හදලම තමයි call කරන්න ඕනේ.
උදාහරණයක් විදියට, Student
Class එකේ displayStudentInfo()
Method එකක් තියෙනවනේ. මේකෙන් කරන්නේ අදාල Student Object එකේ name
, age
, studentId
වගේ Instance Variables ටික display කරන එක. ඒ නිසා මේක Instance Method එකක්.
class Dog {
String name; // Instance Variable
String breed;
public Dog(String name, String breed) {
this.name = name;
this.breed = breed;
}
public void bark() { // Instance Method - operates on instance data (name)
System.out.println(name + " says Woof!");
}
public void displayInfo() { // Instance Method - operates on instance data
System.out.println("Name: " + name + ", Breed: " + breed);
}
}
public class PetHouse {
public static void main(String[] args) {
Dog myDog = new Dog("Buddy", "Golden Retriever");
Dog anotherDog = new Dog("Max", "German Shepherd");
myDog.bark(); // Buddy says Woof!
anotherDog.bark(); // Max says Woof!
myDog.displayInfo(); // Name: Buddy, Breed: Golden Retriever
}
}
myDog.bark()
කියලා කියද්දි Buddy
කියන Dog ගේ bark කරන method එක call වෙනවා. anotherDog.bark()
කියද්දි Max
ගේ bark කරන method එක call වෙනවා. ඒ කියන්නේ මේ Methods Object එකේ data එක්ක තමයි වැඩ කරන්නේ.
Static Methods (Class Methods)
Static Methods කියන්නේ Class එකටම අයිති Methods. මේවා Call කරන්න Object එකක් හදන්න ඕනේ නෑ. Class එකේ නම පාවිච්චි කරලා කෙලින්ම Call කරන්න පුළුවන්. Static Methods වලට Instance Variables පාවිච්චි කරන්න බෑ, මොකද ඒවා Object එකක් එක්ක සම්බන්ධ නැති නිසා. හැබැයි Static Methods ඇතුළේ Static Variables පාවිච්චි කරන්න පුළුවන්.
Static Methods සාමාන්යයෙන් පාවිච්චි කරන්නේ Utility Functions හදන්න, නැත්නම් Class එකටම පොදු operations කරන්න. උදාහරණයක් විදියට, Math Class එකේ තියෙන Math.max()
, Math.min()
වගේ Methods Static Methods. ඒවා call කරන්න Math Object එකක් හදන්න ඕනෙ නෑ.
class Calculator {
static final double PI = 3.14159; // Static Variable
public static int add(int a, int b) { // Static Method
return a + b;
}
public static int subtract(int a, int b) { // Static Method
return a - b;
}
public static double getCircumference(double radius) { // Static Method using static variable
return 2 * PI * radius;
}
}
public class MathOperations {
public static void main(String[] args) {
int sum = Calculator.add(10, 5); // Calling static method using Class name
System.out.println("Sum: " + sum); // Sum: 15
int difference = Calculator.subtract(10, 5);
System.out.println("Difference: " + difference); // Difference: 5
double circum = Calculator.getCircumference(5.0);
System.out.println("Circumference of radius 5: " + circum); // Circumference of radius 5: 31.4159
System.out.println("Value of PI: " + Calculator.PI); // Accessing static variable
}
}
මේ Calculator
Class එකේ තියෙන Methods Call කරන්න අපි new Calculator()
කියලා Object එකක් හැදුවේ නෑ. කෙලින්ම Calculator.add()
වගේ Class එකේ නම පාවිච්චි කරලා Call කරා. මේ වගේ Utility Classes වලට Static Methods ගොඩක්ම ගැලපෙනවා.
කවදාද මොනවා පාවිච්චි කරන්නේ? (When to use what?)
හරි, දැන් අපි Instance සහ Static කියන දෙකම දන්නවා. එතකොට මොන වෙලාවට මොනවා පාවිච්චි කරන්න ඕනද? මේක තීරණය කරන්නේ අපේ requirement එක අනුව. පොඩි guideline එකක් බලමු.
Instance Members (Variables and Methods)
- තනි Object එකක තත්වයක් (state) තියාගන්න ඕනේ නම්:
- උදා:
Student
ගේname
,age
. හැම Student කෙනෙක්ටම වෙනස් නම් සහ වයස් තියෙනවනේ. Car
එකකcolor
,model
. හැම කාර් එකකටම වෙනස් තොරතුරු තියෙනවා.
- උදා:
- තනි Object එකක behaviour එකක් define කරන්න ඕනේ නම්:
- උදා:
Student
කෙනෙක්study()
කරනවා,Dog
කෙනෙක්bark()
කරනවා. මේවා අදාල Object එකටම ආවේණික ක්රියාකාරකම්. Car
එකක්startEngine()
කරනවා. හැම කාර් එකකටම engine එක start කරන්න පුළුවන්, හැබැයි ඒක ඒ කාර් එකටම අදාල දෙයක්.
- උදා:
- සාරාංශයක් විදියට, ඔබ Object එකක හැසිරීමක් හෝ දත්තයක් නිරූපණය කිරීමට උත්සාහ කරන්නේ නම්, එය Instance member එකක් විය යුතුය.
Static Members (Variables and Methods)
- Class එකටම පොදු දත්තයක් තියාගන්න ඕනේ නම්:
- උදා:
schoolName
(හැම Student කෙනෙක්ටම එකම ඉස්කෝලේ නම). totalNumberOfStudents
(මුළු students ගණන, හැම Student Object එකක්ම මේකට contribute කරනවා).- Global Constants:
Math.PI
,System.out
(පොදු දේවල්).
- උදා:
- Object එකක තත්වය මත රඳා නොපවතින ක්රියාකාරකමක් කරන්න ඕනේ නම් (Utility functions):
- උදා:
Math.max(a, b)
,Calculator.add(a, b)
. මේවාට Object එකක් අවශ්ය නෑ, මොකද ඒවා කරන්නේ පොදු ගණනය කිරීම්. - Factory methods: යම් Class එකක Object එකක් හදන්න භාවිතා කරන Methods (උදා:
Connection.getConnection()
).
- උදා:
- ප්රධාන Method එක (main method): Java application එකක් run වෙන්නේ
public static void main(String[] args)
කියන Method එකෙන්. මේකstatic
වෙන්න හේතුව තමයි, application එක run වෙන්න Class එකක Object එකක් හදන්න අවශ්ය නොවීම. - සාරාංශයක් විදියට, ඔබ Object එකක් මත රඳා නොපවතින පොදු දත්තයක් හෝ ක්රියාකාරකමක් නිරූපණය කිරීමට උත්සාහ කරන්නේ නම්, එය Static member එකක් විය යුතුය.
සැලකිය යුතුයි: Static Methods ඇතුළේ Instance Variables හෝ Instance Methods කෙලින්ම Call කරන්න බෑ. මොකද Static Method එකක් Call කරන වෙලාවේ අදාල Object එකක් තියෙන්නත් පුළුවන්, නැතිවෙන්නත් පුළුවන්. ඒත් Static Method එකකට Static Variables සහ Static Methods ඕන වෙලාවක Call කරන්න පුළුවන්.
ප්රායෝගික උදාහරණ (Practical Examples)
හරි, දැන් අපි පොඩ්ඩක් සංකීර්ණ උදාහරණයක් බලමු. අපි Bank Account
එකක් ගැන හිතමු.
class BankAccount {
// Instance Variables: Each account has its own unique data
private String accountNumber;
private String accountHolderName;
private double balance;
// Static Variables: Common to all bank accounts managed by this system
private static int totalAccountsCreated = 0;
private static double BANK_INTEREST_RATE = 0.05; // 5%
// Constructor: To create new bank account instances
public BankAccount(String accountHolderName, double initialBalance) {
this.accountNumber = generateAccountNumber(); // Using a static method (conceptually, could be more complex)
this.accountHolderName = accountHolderName;
this.balance = initialBalance;
totalAccountsCreated++; // Increment static counter
System.out.println("New account created for " + accountHolderName + " with account number " + accountNumber);
}
// Instance Methods: Operate on a specific account's data
public void deposit(double amount) {
if (amount > 0) {
this.balance += amount;
System.out.println("Deposited " + amount + ". New balance: " + this.balance);
} else {
System.out.println("Deposit amount must be positive.");
}
}
public void withdraw(double amount) {
if (amount > 0 && this.balance >= amount) {
this.balance -= amount;
System.out.println("Withdrew " + amount + ". New balance: " + this.balance);
} else if (amount <= 0) {
System.out.println("Withdrawal amount must be positive.");
} else {
System.out.println("Insufficient balance. Current balance: " + this.balance);
}
}
public double getBalance() {
return this.balance;
}
public String getAccountHolderName() {
return this.accountHolderName;
}
// Static Method: Generates a unique account number (example - often more robust in real systems)
// This is static because it doesn't depend on a specific BankAccount instance to generate a number.
private static String generateAccountNumber() {
// In a real system, this would be more complex and unique
return "ACC" + (1000 + totalAccountsCreated + 1); // Simple sequential generation for example
}
// Static Method: To get the total number of accounts
public static int getTotalAccounts() {
return totalAccountsCreated;
}
// Static Method: To change the bank's interest rate
public static void setBankInterestRate(double newRate) {
if (newRate >= 0) {
BANK_INTEREST_RATE = newRate;
System.out.println("Bank interest rate updated to: " + (newRate * 100) + "%");
} else {
System.out.println("Interest rate cannot be negative.");
}
}
// Static Method: To get the current bank interest rate
public static double getBankInterestRate() {
return BANK_INTEREST_RATE;
}
}
public class BankApp {
public static void main(String[] args) {
System.out.println("Initial total accounts: " + BankAccount.getTotalAccounts()); // 0
BankAccount acc1 = new BankAccount("Kasun Perera", 5000.0);
acc1.deposit(1000.0);
acc1.withdraw(200.0);
System.out.println(acc1.getAccountHolderName() + "'s balance: " + acc1.getBalance());
BankAccount acc2 = new BankAccount("Nethmi Fernando", 10000.0);
acc2.withdraw(12000.0); // Insufficient
acc2.deposit(500.0);
System.out.println(acc2.getAccountHolderName() + "'s balance: " + acc2.getBalance());
System.out.println("Current total accounts: " + BankAccount.getTotalAccounts()); // 2
// Change bank-wide interest rate using static method
BankAccount.setBankInterestRate(0.06); // 6%
// Get bank-wide interest rate using static method
System.out.println("Current Bank Interest Rate: " + (BankAccount.getBankInterestRate() * 100) + "%");
}
}
මේ BankAccount
Class එකේ accountNumber
, accountHolderName
, balance
කියන ඒවා Instance Variables. මොකද හැම Account එකකටම මේ දත්ත වෙනස් වෙනස් විදියට තියාගන්න ඕනේ. deposit()
, withdraw()
, getBalance()
කියන ඒවා Instance Methods. මොකද මේවා වැඩ කරන්නේ අදාල Account එකේ Balance එකත් එක්ක.
හැබැයි totalAccountsCreated
සහ BANK_INTEREST_RATE
කියන ඒවා Static Variables. මොකද මේවා මුළු bank system එකටම පොදුයි. ඒ වගේම getTotalAccounts()
, setBankInterestRate()
, getBankInterestRate()
කියන ඒවා Static Methods. මේවාට අදාල Object එකක් ඕනේ නෑ, මුළු Class එකේම data එක්ක තමයි වැඩ කරන්නේ.
generateAccountNumber()
වගේ Method එකක් අපි Static කරලා තියෙන්නේ, Account Number එකක් generate කරන්න Account Object එකක් තියෙන්න ඕනේ නෑ. ඒක Class එකටම අදාල Utility function එකක් විදියට පාවිච්චි කරන්න පුළුවන් නිසා.
නිගමනය (Conclusion)
ඔන්න ඉතින් යාළුවනේ, අපි Class Methods සහ Variables ගැන හොඳට කතා කළා. දැන් ඔයාලට පැහැදිලියි නේද Instance (non-static) සහ Static කියන දෙකේ වෙනස සහ ඒවා මොන වෙලාවටද පාවිච්චි කරන්නේ කියලා?
- සරලවම කිව්වොත්, Object එකකටම ආවේණික දේවල් Instance.
- Class එකටම පොදු දේවල් Static.
මේක තේරුම් ගැනීම Object-Oriented Programming වලදී ගොඩක් වැදගත්. මේ concept එක clarify කරගත්තොත්, ඔයාට cleaner, maintainable, efficient code ලියන්න පුළුවන්. ඒ වගේම design patterns තේරුම් ගන්නත් මේක උදව් වෙනවා.
අද ඉගෙනගත්ත දේවල් ඔයාලම try කරලා බලන්න. පොඩි පොඩි Java programs ලියලා Instance සහ Static members පාවිච්චි කරලා බලන්න. එතකොට ඔයාලට මේක තවත් හොඳට තේරෙයි.
මේ ලිපිය ගැන ඔයාලගේ අදහස්, ප්රශ්න පහළ comment section එකේ දාන්න. අපි ඒවට උත්තර දෙන්නම්. ඒ වගේම මේ ලිපිය ඔයාගේ Java යාළුවන්ටත් share කරන්න අමතක කරන්න එපා. තවත් මේ වගේ වැදගත් ලිපියකින් හමුවෙමු! Happy Coding!