在Java中, 当一个类被调用的时候,它的加载过程是怎么样的呢? 当一个类被实例化的时候,它的加载过程又是怎样的呢? 为什么static方法不能未经对象就调用非static方法? 下面我们通过例子来找出答案。 当该类没有父类时 测试代码: public class C ...
在Java中,
当一个类被调用的时候,它的加载过程是怎么样的呢?
当一个类被实例化的时候,它的加载过程又是怎样的呢?
为什么static方法不能未经对象就调用非static方法?
下面我们通过例子来找出答案。
当该类没有父类时
测试代码:
public class ClassLoadingTest { static String staticString=getStaticString(); //静态成员变量 private String privateString=getPrivateString();//普通成员变量 public ClassLoadingTest() { System.out.println("Loading ClassLoadingTest()"); } public static void loadStaticMethod() { System.out.println("Loading static method"); } private void loadNotStaticMethed(){ System.out.println("Loading NotStaticMethed"); } private String getPrivateString() { String string=new String("Loading private variable"); System.out.println(string); return string; } private static String getStaticString() { String string=new String("Loading static variable"); System.out.println(string); return string; } public static void main(String[] args) { loadStaticMethod(); //加载静态方法 System.out.println("try to new ClassLoadingTest()"); ClassLoadingTest test=new ClassLoadingTest(); test.loadNotStaticMethed();//加载普通方法 }}
原标题:类和对象的加载过程
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。