មេរៀនទី១៣: static keyword
ពាក្ស static keyword ត្រូវបានប្រើក្នុង java សម្រាប់គ្រប់គ្រង
memory។ យើងអាចអនុវត្តន៍ static keyword ជាមួយអថេរ, methods, blocks និង
class។ ដែល static អាចជា:
1) អថេរ static variable
ប្រសិនបើលោកអ្នក ប្រកាស អថេរដូចជា static វាដឹងថាអថេរ static ។
• អថេរ static អាចប្រើដើម្បី ប្រើ property រួមនៃ objects ទាំងអស់ ឧទាហរណ៍ ឈ្មោះក្រុមហ៊ុន របស់ employees ឈ្មោះមហាវិទ្យាល័យនៃនិស្សិត ។
• អថេរ static ទទួល memory នៅក្នុង class ពេលដែល class loading។
សារ:សំខាន់នៃអថេរ static
ស្វែងយល់ពីបញ្ហា ដោយមិនប្រើអថេរ static variable
class Student{
int rollno;
String name;
String college=”ITS”;
}
ឧបមាថាមាន 500 និស្សិតក្នុងមហាវិទ្យាល័យ , instance data នឹងទទួល memory កាលណា object ត្រូវបានបង្កើត។
static property ត្រូវបានចែកចាយ objects ទាំងអស់
ឧទាហរណ៍នៃអថេរ static variable
//Program of static variable
class Student8{
int rollno;
String name;
static String college =”ITS”;
Student8(int r,String n){
rollno = r;
name = n;
}
void display (){System.out.println(rollno+” “+name+” “+college);}
public static void main(String args[]){
Student8 s1 = new Student8(111,”Karan”);
Student8 s2 = new Student8(222,”Aryan”);
s1.display();
s2.display();
}
}
Output:111 Karan ITS
222 Aryan ITS
កម្មវិធីនៃ counter ដោយមិនប្រើអថេរ static variable
ដូច្នេះ object និមួយៗនឹងមានតម្លៃ 1 ក្នុងអថេរ count។
class Counter{
int count=0;//will get memory when instance is created
Counter(){
count++;
System.out.println(count);
}
public static void main(String args[]){
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
Output:1
1
1
កម្មវិធី counter ដោយប្រើអថេរ static
ដូចដែលបាន ពន្យល់ខាងលើ static អថេរនឹងទទួល memory មួយគត់ ប្រសិនបើ object ផ្សេងទៀតផ្លាស់ប្តូរតម្លៃនៃអថេរ static variable។
class Counter2{
static int count=0;//will get memory only once and retain its value
Counter2(){
count++;
System.out.println(count);
}
public static void main(String args[]){
Counter2 c1=new Counter2();
Counter2 c2=new Counter2();
Counter2 c3=new Counter2();
}
}
Output:1
2
3
2) static method
ប្រសិនបើលោកអ្នកអនុវត្ត static keyword ជាមួយ method ផ្សេងទៀត វាដឹងថាដូចជា static method
• static method កម្មសិទ្ធ class ដូចជា object របស់ class មួយ
• static method អាចមិនប្រើ ត្រូវការបង្កើត class
• static method អាចអនុញ្ញាត static data member និងអាចផ្លាស់ប្តូរតម្លៃរបស់វា។
ឧទាហរណ៍នៃ static method
//Program of changing the common property of all objects(static field).
class Student9{
int rollno;
String name;
static String college = “ITS”;
static void change(){
college = “BBDIT”;
}
Student9(int r, String n){
rollno = r;
name = n;
}
void display (){System.out.println(rollno+” “+name+” “+college);}
public static void main(String args[]){
Student9.change();
Student9 s1 = new Student9 (111,”Karan”);
Student9 s2 = new Student9 (222,”Aryan”);
Student9 s3 = new Student9 (333,”Sonoo”);
s1.display();
s2.display();
s3.display();
}
}
Output:111 Karan BBDIT
222 Aryan BBDIT
333 Sonoo BBDIT
ឧទាហរណ៍ផ្សេងទៀតនៃ static method //Program to get cube of a given number by static method
class Calculate{
static int cube(int x){
return x*x*x;
}
public static void main(String args[]){
int result=Calculate.cube(5);
System.out.println(result);
}
}
Output:125
ការកំណត់លក្ខខ័ណ្ឌសម្រាប់ static method
មានពីវិធីសម្រាប់កំណត់ static method:
static method មិនអាចប្រើ static data member ឬហៅ non-static method ដោយផ្ទាល់។
class A{
int a=40;//non static
public static void main(String args[]){
System.out.println(a);
}
}
Output:Compile Time Error
3)static block
• ប្រើសម្រាប់ចាប់ផ្តើម static data member.
• វាបានប្រត្តិបត្តិការ មុនពេល method ពេល class loading។
ឧទាហរណ៍នៃ static block
class A2{
static{System.out.println(“static block is invoked”);}
public static void main(String args[]){
System.out.println(“Hello main”);
}
}
Output:static block is invoked
Hello main
ប្រត្តិបត្តិកម្មវិធីដោយមិនប្រើ main() method?
static block អាចប្រើជាមួយ JDK មិនប្រើក្នុង JDK 1.7.
class A3{
static{
System.out.println(“static block is invoked”);
System.exit(0);
}
}
Output:static block is invoked (if not JDK7)
In JDK7 and above, output will be:
Output:Error: Main method not found in class A3, please define the main method as:
public static void main(String[] args)
1) អថេរ static variable
ប្រសិនបើលោកអ្នក ប្រកាស អថេរដូចជា static វាដឹងថាអថេរ static ។
• អថេរ static អាចប្រើដើម្បី ប្រើ property រួមនៃ objects ទាំងអស់ ឧទាហរណ៍ ឈ្មោះក្រុមហ៊ុន របស់ employees ឈ្មោះមហាវិទ្យាល័យនៃនិស្សិត ។
• អថេរ static ទទួល memory នៅក្នុង class ពេលដែល class loading។
សារ:សំខាន់នៃអថេរ static
ស្វែងយល់ពីបញ្ហា ដោយមិនប្រើអថេរ static variable
class Student{
int rollno;
String name;
String college=”ITS”;
}
ឧបមាថាមាន 500 និស្សិតក្នុងមហាវិទ្យាល័យ , instance data នឹងទទួល memory កាលណា object ត្រូវបានបង្កើត។
static property ត្រូវបានចែកចាយ objects ទាំងអស់
ឧទាហរណ៍នៃអថេរ static variable
//Program of static variable
class Student8{
int rollno;
String name;
static String college =”ITS”;
Student8(int r,String n){
rollno = r;
name = n;
}
void display (){System.out.println(rollno+” “+name+” “+college);}
public static void main(String args[]){
Student8 s1 = new Student8(111,”Karan”);
Student8 s2 = new Student8(222,”Aryan”);
s1.display();
s2.display();
}
}
Output:111 Karan ITS
222 Aryan ITS

ដូច្នេះ object និមួយៗនឹងមានតម្លៃ 1 ក្នុងអថេរ count។
class Counter{
int count=0;//will get memory when instance is created
Counter(){
count++;
System.out.println(count);
}
public static void main(String args[]){
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
Output:1
1
1
កម្មវិធី counter ដោយប្រើអថេរ static
ដូចដែលបាន ពន្យល់ខាងលើ static អថេរនឹងទទួល memory មួយគត់ ប្រសិនបើ object ផ្សេងទៀតផ្លាស់ប្តូរតម្លៃនៃអថេរ static variable។
class Counter2{
static int count=0;//will get memory only once and retain its value
Counter2(){
count++;
System.out.println(count);
}
public static void main(String args[]){
Counter2 c1=new Counter2();
Counter2 c2=new Counter2();
Counter2 c3=new Counter2();
}
}
Output:1
2
3
2) static method
ប្រសិនបើលោកអ្នកអនុវត្ត static keyword ជាមួយ method ផ្សេងទៀត វាដឹងថាដូចជា static method
• static method កម្មសិទ្ធ class ដូចជា object របស់ class មួយ
• static method អាចមិនប្រើ ត្រូវការបង្កើត class
• static method អាចអនុញ្ញាត static data member និងអាចផ្លាស់ប្តូរតម្លៃរបស់វា។
ឧទាហរណ៍នៃ static method
//Program of changing the common property of all objects(static field).
class Student9{
int rollno;
String name;
static String college = “ITS”;
static void change(){
college = “BBDIT”;
}
Student9(int r, String n){
rollno = r;
name = n;
}
void display (){System.out.println(rollno+” “+name+” “+college);}
public static void main(String args[]){
Student9.change();
Student9 s1 = new Student9 (111,”Karan”);
Student9 s2 = new Student9 (222,”Aryan”);
Student9 s3 = new Student9 (333,”Sonoo”);
s1.display();
s2.display();
s3.display();
}
}
Output:111 Karan BBDIT
222 Aryan BBDIT
333 Sonoo BBDIT
ឧទាហរណ៍ផ្សេងទៀតនៃ static method //Program to get cube of a given number by static method
class Calculate{
static int cube(int x){
return x*x*x;
}
public static void main(String args[]){
int result=Calculate.cube(5);
System.out.println(result);
}
}
Output:125
ការកំណត់លក្ខខ័ណ្ឌសម្រាប់ static method
មានពីវិធីសម្រាប់កំណត់ static method:
static method មិនអាចប្រើ static data member ឬហៅ non-static method ដោយផ្ទាល់។
class A{
int a=40;//non static
public static void main(String args[]){
System.out.println(a);
}
}
Output:Compile Time Error
3)static block
• ប្រើសម្រាប់ចាប់ផ្តើម static data member.
• វាបានប្រត្តិបត្តិការ មុនពេល method ពេល class loading។
ឧទាហរណ៍នៃ static block
class A2{
static{System.out.println(“static block is invoked”);}
public static void main(String args[]){
System.out.println(“Hello main”);
}
}
Output:static block is invoked
Hello main
ប្រត្តិបត្តិកម្មវិធីដោយមិនប្រើ main() method?
static block អាចប្រើជាមួយ JDK មិនប្រើក្នុង JDK 1.7.
class A3{
static{
System.out.println(“static block is invoked”);
System.exit(0);
}
}
Output:static block is invoked (if not JDK7)
In JDK7 and above, output will be:
Output:Error: Main method not found in class A3, please define the main method as:
public static void main(String[] args)
Post a Comment