码迷,mamicode.com
首页 > 编程语言 > 详细

java初始化

时间:2019-03-03 16:05:55      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:system   pre   col   开始   new   初始   ati   static   存储空间   

java中一切皆对象,那就从对象初始化开始说起。

 示例代码:

 1 package test;
 2 
 3 public class A extends B {
 4 
 5     int i;
 6     static int b;
 7     public D firstD = new D("A");
 8     public static C firstC = new C("A");
 9     public static C secondC;
10     {
11         System.out.println("i:"+i);//此时i未初始化,为默认值0
12         System.out.println("b:"+b);
13         System.out.println(secondC);
14     }
15     A(){
16         
17         System.out.println("here is A");
18         i = 1;
19         b = 1;
20     }
21     
22     public static void main(String[] args) {
23         // TODO 自动生成的方法存根
24         new A();
25     }
26 
27 }
28 
29 class B {
30     public static C CinB = new C("B");
31     public D DinB = new D("B");
32     B(){
33         System.out.println("here is B");
34     }
35 }
36 
37 class C {
38     C(String source){
39         System.out.println("here is C from"+source);
40     }
41 }
42 
43 class D {
44     D(String source){
45         System.out.println("here is D from"+source);
46     }
47 }

 

输出:

here is C fromB
here is C fromA
here is D fromB
here is B
here is D fromA
i:0
b:0
null
here is A

 

结论:

1.在初始化时,首先为对象分配一块存储空间(默认为二进制0)

2.初始化顺序:基类静态成员->子类静态成员->基类非静态成员/实例初始化->基类构造器->子类非静态成员/实例初始化->子类构造器

java初始化

标签:system   pre   col   开始   new   初始   ati   static   存储空间   

原文地址:https://www.cnblogs.com/xiaowk/p/10465818.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!