Java 初始化过程详解
2006-10-23 09:39
543 查看
觉得Core Java在Java 初始化过程的总体顺序没有讲,只是说了构造器时的顺序,作者似乎认为路径很多,列出来比较混乱.我觉得还是要搞清楚它的过程比较好.所以现在结合我的学习经验写出具体过程:
过程如下:
1.在类的声明里查看有无静态元素(static element, 我姑且这么叫吧),比如static int x = 1,
{//block
float sss = 333.3; String str = "hello";
}
或者 比如
static { //(static block),
int x = 2;
double y = 33.3;
}
如果有static element则首先执行其中语句,但注意static element只执行一次,在第二次创建类的对象的时候,就不会去执行static element的语句.
2.查看此类是否为启动运行类,若为启动运行类,则执行main()方法里的语句对应语句
3.若不是启动运行类,则按代码的排版先后顺序继续执行非static element的变量赋值以及代码块.
4.最后执行构造方法,如果在被调用的构造方法里面有this关键字(注意,如果你考虑要调用其他构造方法,则应该把this写在最前面,不然会产生错误),则先调用相应构造方法主体,调用完之后再执行自己的剩下语句.
/** *//**
*
* @author livahu
* Created on 2006年9月6日, 下午17:00
*/
class FirstClass ...{
FirstClass(int i) ...{
System.out.println("FirstClass(" + i + ")");
}
void useMethod(int k) ...{
System.out.println("useMethod(" + k + ")");
}
}
class SecondClass ...{
static FirstClass fc1 = new FirstClass(1);
FirstClass fc3 = new FirstClass(3);
static ...{
FirstClass fc2 = new FirstClass(2);
}
...{
System.out.println("SecondClass's block, this block is not static block.");
}
SecondClass() ...{
System.out.println("SecondClass()");
}
FirstClass fc4 = new FirstClass(4);
}
public class InitiationDemo ...{
SecondClass sc1 = new SecondClass();
...{
System.out.println("Hello Java World!");
}
public static void main(String[] args) ...{
System.out.println("Inside main()");
SecondClass.fc1.useMethod(100);
InitiationDemo idObj = new InitiationDemo();
}
static SecondClass sc2 = new SecondClass();
}
运行结果:
FirstClass(1)
FirstClass(2)
FirstClass(3)
SecondClass's block, this block is not static block.
FirstClass(4)
SecondClass()
Inside main()
useMethod(100)
FirstClass(3)
SecondClass's block, this block is not static block.
FirstClass(4)
SecondClass()
Hello Java World!
过程如下:
1.在类的声明里查看有无静态元素(static element, 我姑且这么叫吧),比如static int x = 1,
{//block
float sss = 333.3; String str = "hello";
}
或者 比如
static { //(static block),
int x = 2;
double y = 33.3;
}
如果有static element则首先执行其中语句,但注意static element只执行一次,在第二次创建类的对象的时候,就不会去执行static element的语句.
2.查看此类是否为启动运行类,若为启动运行类,则执行main()方法里的语句对应语句
3.若不是启动运行类,则按代码的排版先后顺序继续执行非static element的变量赋值以及代码块.
4.最后执行构造方法,如果在被调用的构造方法里面有this关键字(注意,如果你考虑要调用其他构造方法,则应该把this写在最前面,不然会产生错误),则先调用相应构造方法主体,调用完之后再执行自己的剩下语句.
/** *//**
*
* @author livahu
* Created on 2006年9月6日, 下午17:00
*/
class FirstClass ...{
FirstClass(int i) ...{
System.out.println("FirstClass(" + i + ")");
}
void useMethod(int k) ...{
System.out.println("useMethod(" + k + ")");
}
}
class SecondClass ...{
static FirstClass fc1 = new FirstClass(1);
FirstClass fc3 = new FirstClass(3);
static ...{
FirstClass fc2 = new FirstClass(2);
}
...{
System.out.println("SecondClass's block, this block is not static block.");
}
SecondClass() ...{
System.out.println("SecondClass()");
}
FirstClass fc4 = new FirstClass(4);
}
public class InitiationDemo ...{
SecondClass sc1 = new SecondClass();
...{
System.out.println("Hello Java World!");
}
public static void main(String[] args) ...{
System.out.println("Inside main()");
SecondClass.fc1.useMethod(100);
InitiationDemo idObj = new InitiationDemo();
}
static SecondClass sc2 = new SecondClass();
}
运行结果:
FirstClass(1)
FirstClass(2)
FirstClass(3)
SecondClass's block, this block is not static block.
FirstClass(4)
SecondClass()
Inside main()
useMethod(100)
FirstClass(3)
SecondClass's block, this block is not static block.
FirstClass(4)
SecondClass()
Hello Java World!
相关文章推荐
- Java 初始化过程详解
- java类的初始化过程详解
- java程序初始化过程详解
- java类的初始化过程详解
- JAVA之对象初始化过程详解及面试题
- Java 程序初始化过程详解 -转
- Java中对象的初始化过程详解
- JAVA面向对象知识点总结(3)—静态static、主函数详解、静态代码快、对象初始化过程
- 黑马程序员——Java程序的初始化过程详解
- JAVA初始化过程
- java类对象初始化过程
- 解析 Java 类和对象的初始化过程
- 关于Java中类在构造对象时的初始化步骤详解
- Java中成员变量的初始化过程
- 深入理解Java对象的创建过程:类的初始化与实例化
- java学习笔记_类建立对象初始化过程分析
- JAVA的初始化过程
- Java:对象创建和初始化过程
- Java类的初始化过程
- java代码的初始化过程研究