方法1:A a=new test().new A(); 内部类对象通过外部类的实例对象调用其内部类构造方法产生,如下
- public class test{
- class A{
- void fA(){
- System.out.println("we are students");
- }
- }
- public static void main(String args[]){
- System.out.println("Hello, 欢迎学习JAVA");
- A a=new test().new A();
- a.fA();
- }
- }
方法2: fA()方法设为静态方法。 当主类加载到内存,fA()分配了入口地址。
- public class test{
- static void fA(){
- System.out.println("we are students");
- }
- public static void main(String args[]){
- System.out.println("Hello, 欢迎学习JAVA");
- fA();
- }
- }
- public class test{
- public static void main(String args[]){
- System.out.println("Hello, 欢迎学习JAVA");
- A a=new A();
- a.fA();
- }
- }
- class A{
- void fA(){
- System.out.println("we are students");
- }
- }
原文来自:http://blog.csdn.net/huacuo2013/article/details/45248841