将非静态方法设为静态方法的参数,然后在静态方法中调用该参数。 publicclassMyClass{publicstaticvoidstaticMethod(){// 调用静态方法nonStaticMethod();}publicstaticvoidnonStaticMethod(){// 非静态方法的实现}} 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 使用上述代码,我们将非静态方法nonStaticMethod()设为静态...
1、静态内部类 静态内部类桶静态代码块一样,只能访问外部类的静态方法和属性。 public class Test { private static long staticValue = System.currentTimeMillis(); private String value = "普通属性"; static{ System.out.println("静态代码块:"+System.currentTimeMillis()); } {System.out.println("构造...
1. 创建一个类 首先,我们创建一个类TestClass,其中包含一个非静态变量nonStaticVar和一个静态方法staticMethod。 publicclassTestClass{intnonStaticVar=10;publicstaticvoidstaticMethod(){// 在这里尝试访问非静态变量nonStaticVar// 非静态变量无法在静态方法中直接访问}} 1. 2. 3. 4. 5. 6. 7. 8. 2. ...