`
backkom1982
  • 浏览: 28038 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

探究java的ClassLoader及类变量初始化顺序

    博客分类:
  • java
阅读更多
[list]
  • 通过直接调用构造函数:

  • Backkom instance1=new Backkom();


    如果当前ClassLoader无法找到Backkom类,抛出NoClassDefFoundError。
  • 反射机制1:

  • Class clazz = Class.forName("Backkom");
    Object instance2 =clazz.newInstance();
    


    如果无法找到Backkom,抛出ClassNotFoundException。
  • 反射机制2:

  • Class clazz = classLoader.loadClass("Backkom");
    Object instance2 =clazz.newInstance();
    


    如果无法找到Backkom,抛出ClassNotFoundException。
    [/list]

    方法1和方法2是从当前的ClassLoader中去加载,方法3是从用户指定的ClassLoader中加载。方法2和方法3在初始化变量时的一个显著不同是:使用方法2,类的静态变量和静态化初始块在执行Class.forName()时执行;使用方法3,类的静态变量和静态初始块在执行Class.newInstance()时执行。

    JLS中给出的Java类对象初始化顺序定义如下:
    1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
    2. If this constructor begins with an explicit constructor invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
    3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
    4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5. (In some early implementations, the compiler incorrectly omitted the code to initialize a field if the field initializer expression was a constant expression whose value was equal to the default initialization value for its type.)
    5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.



    对类对象初始化顺序的部分解释如下:
    1. 为类分配内存空间,初始化所有成员变量为默认值,包括primitive类型(int=0,boolean=false,…)和Reference类型。
    2. 如继承与某个父类,调用父类的默认构造函数
    3. 调用当前类的默认构造函数

    类对象初始化时需要特别注意:成员变量初始化是在父类构造函数调用完后,在此之前,成员变量的值均是默认值。故在构造函数中尽量不要加入复杂的业务逻辑,亦可凡是不使用复杂的继承关系,从而减少怪异问题的出现。
    0
    0
    分享到:
    评论

    相关推荐

    Global site tag (gtag.js) - Google Analytics