មេរៀនទី៥៧: static nested class
static class មួយដែលត្រូវបានបង្កើតក្នុង class មួយគឺជា static nested class។ វាមិនអាច អនុញ្ញាតប្រើ non-static members
• វាអាចអនុញ្ញាត access static data members នៃ outer class ដែលរួម private.
• static nested class មិនអាចអនុញ្ញាត non-static (instance) data member ឬ method.
កម្មវិធីនៃ static nested class that have instance method
class TestOuter1{
static int data=30;
static class Inner{
void msg(){System.out.println(“data is “+data);}
}
public static void main(String args[]){
TestOuter1.Inner obj=new TestOuter1.Inner();
obj.msg();
}
}
Output:data is 30
Internal code បង្កើតដោយ compiler សម្រាប់ static nested class
import java.io.PrintStream;
static class Outer$Inner
{
Outer$Inner(){}
void msg(){
System.out.println((new StringBuilder()).append(“data is “)
.append(Outer.data).toString());
}
}
កម្មវិធី static nested class ដែលមាន static method
class TestOuter2{
static int data=30;
static class Inner{
static void msg(){System.out.println(“data is “+data);}
}
public static void main(String args[]){
TestOuter2.Inner.msg();//no need to create the instance of static nested class
}
}
Output:data is 30
• វាអាចអនុញ្ញាត access static data members នៃ outer class ដែលរួម private.
• static nested class មិនអាចអនុញ្ញាត non-static (instance) data member ឬ method.
កម្មវិធីនៃ static nested class that have instance method
class TestOuter1{
static int data=30;
static class Inner{
void msg(){System.out.println(“data is “+data);}
}
public static void main(String args[]){
TestOuter1.Inner obj=new TestOuter1.Inner();
obj.msg();
}
}
Output:data is 30
Internal code បង្កើតដោយ compiler សម្រាប់ static nested class
import java.io.PrintStream;
static class Outer$Inner
{
Outer$Inner(){}
void msg(){
System.out.println((new StringBuilder()).append(“data is “)
.append(Outer.data).toString());
}
}
កម្មវិធី static nested class ដែលមាន static method
class TestOuter2{
static int data=30;
static class Inner{
static void msg(){System.out.println(“data is “+data);}
}
public static void main(String args[]){
TestOuter2.Inner.msg();//no need to create the instance of static nested class
}
}
Output:data is 30
Post a Comment