public class OuterClass{
public class InnerClass{
public void print(){
System.out.println("this a innerclass");
}
}
public void fun(){
new InnerClass().print();
}
public static void main(String[] args){
OuterClass.InnerClass p = new OuterClass().new InnerClass();
p.print();
new OuterClass().fun();
System.out.println("this is a outerclass");
}
}
外部类调用静态内部类中的非静态方法:
public class OuterClass{
public static class InnerClass{
public InnerClass(){
System.out.println("this is a innerclass");
}
public void print(String str){
System.out.println(str);
}
}
public static void main(String ages[]){
OuterClass.InnerClass ci = new OuterClass.InnerClass();
ci.print("this is a string");
}
}
外部类调用静态内部类中的静态方法:
public class OuterClass{
public static class InnerClass{
public static void print(String str){
System.out.println(str);
}
}
public void fun(String str){
InnerClass.print(str);
}
public static void main(String[] args){
InnerClass.print("this is a string");
new OuterClass().fun("this is a another string");
}
}