标签:赋值 null detail 静态代码块 div java对象 查看 代码块 lang
class A {
public A() {
init();
}
public void init() {
}
public static void main(String[] args) {
B b = new B();
System.out.println("终于i的值为:" + b.i + ",j的值为:" + b.j);
}
}
class B extends A {
int i;
int j = 999;
public void init() {
System.out.println("此时A的构造方法正在调用此方法:i的值为:" + i + ",j的值为:" + j);
i = 888;
j = 111;
}
}看看打印什么此时A的构造方法正在调用此方法:i的值为:0,j的值为:0 终于i的值为:888,j的值为:999假设感到非常吃惊,那么你对Java对象的构造过程还不熟悉。那么认真阅读本文将对你有非常大帮助。
public class Super {
static long time = 10;
static Object obj = new Object();
int width = 100;
static {
time = 11;
}
{
width = 110;
}
public Super() {
width = 120;
}
public static void main(String[] args) {
Child child = new Child();
System.out.println("Super.time:"+Super.time);
System.out.println("Super.obj:"+Super.obj);
System.out.println("child.width:"+child.width);
System.out.println("Child.age:"+Child.age);
System.out.println("Child.str:"+Child.str);
System.out.println("child.height:"+child.height);
}
}
class Child extends Super {
static int age = 20;
static String str = "str";
double height = 200;
static {
age = 22;
}
{
height = 210;
}
public Child() {
height = 220;
}
}
打印
Super.time:11 Super.obj:java.lang.Object@659e0bfd child.width:120 Child.age:22 Child.str:str child.height:220.0
public class Super {
static long time=10;//此时time=0
static Object obj=new Object();//此时obj=nullclass Child extends Super{
static int age=20;//此时age=0
static String str="str";//此时str=nullpublic class Super {
static long time=10;//此时time=10
static Object obj=new Object();//此时obj=new Object()public class Super {
static long time=10;
static Object obj=new Object();
int width=100;
static{
time=11;//静态代码块运行了。这个时候time=11
}class Child extends Super{
static int age=20;//此时age=20
static String str="str";//此时str="str"
class Child extends Super{
static int age=20;
static String str="str";
double height=200;
static{
age=22;//此时age=22
}
public class Super {
static long time=10;
static Object obj=new Object();
int width=100;//此时width=0class Child extends Super{
static int age=20;
static String str="str";
double height=200;//此时height=0.0public class Super {
static long time=10;
static Object obj=new Object();
int width=100;//此时width=100public class Super {
static long time=10;
static Object obj=new Object();
int width=100;
static{
time=11;
}
{
width=110;//此时width=110
}public class Super {
static long time=10;
static Object obj=new Object();
int width=100;
static{
time=11;
}
{
width=110;
}
public Super() {
width=120;//此时width=120
}class Child extends Super{
static int age=20;
static String str="str";
double height=200;//此时height=200.0class Child extends Super{
static int age=20;
static String str="str";
double height=200;
static{
age=22;
}
{
height=210;//此时height=210.0
}class Child extends Super{
static int age=20;
static String str="str";
double height=200;
static{
age=22;
}
{
height=210;
}
public Child() {
height=220;//此时height=220.0
}对象构造完毕!
比如:
我们在A类中定义一个B类的引用。
public class Super {
public static void main(String[] args) {
new A();new B();
}
}
class A{
static B b=new B();//这句代码会导致B类会比A类先初始化完毕,也就是说B的静态属性会先赋值,静态代码块会先运行。
static {
System.out.println("AA");
}
}
class B{
static {
System.out.println("BB");
}
}BB AA
public class Super {
public static void main(String[] args) {
new A();
}
}
class A{
static B b;
static {
System.out.println("AA");
}
}
class B{
static {
System.out.println("BB");
}
}AA
关于一个人在什么情况才算是主动使用请查看我的还有一篇文章:
http://blog.csdn.net/u012643122/article/details/46522345
如
public class Super {
public static void main(String[] args) {
new A();
}
}
class A{
static B b=new B();
static {
System.out.println("AA");
}
}
class B{
static A a=new A();
static {
System.out.println("BB");
}
}BB AA
如:
static long time;//保持之前所赋的默认值0 Child child;//保持之前所赋的默认值null
假设一个类没有父类(如Object类),则它的初始化顺序能够简化成2、5、6、8、12、13、14。
假设这个类已经被类载入器载入过了,也就是该类的静态部分已经初始化过了,那么1、2、3、4、5、6都不会运行,总的顺序能够简化为7、8、9、10、11、12、13、14。
假设这个类没有被类载入器载入,但它的父类已经被类载入器载入过了。那么总的顺序能够简化为2、5、6、7、8、9、10、11、12、13、14。
转载请标明原地址。请尊重原创,谢谢!
标签:赋值 null detail 静态代码块 div java对象 查看 代码块 lang
原文地址:http://www.cnblogs.com/gccbuaa/p/6715258.html