java学习之实例变量初始化

实例变量的初始化方法

第一种:通过构造函数进行初始化。

第二种:通过声明实例字段初始化。

第三种:通过对象代码块初始化。

通过构造函数进行初始化方法

通过构造函数进行对象初始化,必须在类中声明一个带参数的构造函数。从而通过类创建实例的同时对实例变量进行初始化。注:如果没有声明带参数的构造函数,调用默认构造函数,默认构造函数也对实例变量进行了默认初始化。例如:

 1 package com.java.test;
 2 
 3 
 4 
 5 class Product {
 6             private int id;
 7              private String name;
 8              
 9              public Product() {
10                     
11                 }
12              
13               public Product(int id, String name) {
14                     this.id = id;
15                     this.name = name;
16                 }
17               public String toString() 
18               {
19                   return id+","+name;
20               }
21               
22               public void print() 
23               {
24                   System.out.println(toString());
25               }
26 }
27 
28 public class TestProduct
29 {
30     public static void main(String[]args) 
31     {
32         Product c=new Product();
33         c.print();
34         //结果是0,null
35         Product s=new Product(10,"apple");
36         s.print();
37         //结果是10,apple
38         
39     }
40 }

通过声明实例字段进行初始化方法

通过实例变量声明实例变量就是在创建类的时候,对实例变量进行初始化。例如:

 1 class SomeClass
 2 {
 3    static boolean b;
 4    static byte by;
 5    static char c;
 6    static double d;
 7    static float f;
 8    static int i;
 9    static long l;
10    static short s;
11    static String st;
12 }

初始化结果为

false
0
u0000
0.0
0.0
0
0
0
null

通过对象代码块进行初始化方法

对象代码块初始化是在类中声明代码块来进行声明实例变量。对象代码块的执行是在对象执行构造函数前执行。对象代码块前没有static关键字,加static关键字就变为类代码块。下面通过一个例子来说明:

 1 package test;
 2 
 3 
 4 
 5 class Product {
 6             private int id;
 7              private String name;
 8              
 9              public Product() {
10                     
11                 }
12              
13               public Product(int id, String name) {
14                     this.id = id;
15                     this.name = name;
16                 }
17               public String toString() 
18               {
19                   return id+","+name;
20               }
21               {
22                   name="Sharplee";
23               }
24               public void print() 
25               {
26                   System.out.println(toString());
27               }
28               
29               {
30                   System.out.println("id is "+id);
31                   System.out.println("name is "+name);
32               }
33               
34 }
35 
36 public class TestProduct
37 {
38     public static void main(String[]args) 
39     {
40         Product c=new Product();//id is0    name isSharplee
41         c.print();//0,Sharplee
42         
43         Product s=new Product(10,"apple");//id is0   name isSharplee
44         s.print();//10,apple
45         
46         
47     }
48 }

通过该代码能够看出代码块执行也是从上到下的顺序,并且代码块执行是在构造函数之前。代码块的出现,是便于匿名类来使用的。匿名类是不创建构造函数的。因此在初始化变量的时候,可以使用代码块。

类代码块

类代码块就是在加载类的时候进行初始化。例如:

 1 package test;
 2 
 3 
 4 
 5 class Product {
 6             private static int price;
 7             private int id;
 8              private String name;
 9              
10              public Product() {
11                     
12                 }
13               static 
14               {
15                   price=100;
16                   System.out.println("the price is:"+price);
17               }
18               public Product(int id, String name,int price) {
19                     this.id = id;
20                     this.name = name;
21                     this.price=price;
22                 }
23               public String toString() 
24               {
25                   return id+","+name+","+price;
26               }
27               {
28                   name="Sharplee";
29               }
30               public void print() 
31               {
32                   System.out.println(toString());
33               }
34               
35               {
36                   System.out.println("id is "+id);
37                   System.out.println("name is "+name);
38                   System.out.println(price);
39               }
40               
41 }
42 
43 public class TestProduct
44 {
45     public static void main(String[]args) 
46     {
47         Product c=new Product();
48         c.print();
49         
50         Product s=new Product(10,"apple",300);
51         s.print();
52         
53     
54         
55     }
56 }

类代码块以及块代码块的区别

类代码块无论创建多少个对象都只初始化一次,而对象代码块在创建的对象的时候都执行。

类代码块初始化必须在前面加关键字static,而对象代码块则不用加。

类代码块只能使用类变量进行初始化以及在代码块中声明变量,而对象代码块则没有限制。

 1 package test;
 2 
 3 
 4 
 5 class Product {
 6             private static int price;
 7             private int id;
 8              private String name;
 9              
10              public Product() {
11                     
12                 }
13               static 
14               {
15 16                   price=100;
17                   System.out.println("the price is:"+price);
18               }
19               static
20               {
21                   price++;
22               }
23               
24               public Product(int id, String name,int price) {
25                     this.id = id;
26                     this.name = name;
27                     this.price=price;
28                 }
29               public String toString() 
30               {
31                   return id+","+name+","+price;
32               }
33               {
34                   name="Sharplee";
35               }
36               public void print() 
37               {
38                   System.out.println(toString());
39               }
40               
41               {
42                   System.out.println("id is "+id);
43                   System.out.println("name is "+name);
44                   System.out.println(price);
45               }
46               
47 }
48 
49 public class TestProduct
50 {
51     public static void main(String[]args) 
52     {
53         Product p=null;
54         Product t=new Product();
55         Product c=new Product();
56     
57         
58     }
59 }
View Code
 1 package test;
 2 
 3 
 4 
 5 class Product {
 6             private static int price;
 7             private int id;
 8              private String name;
 9              
10              public Product() {
11                     
12                 }
13               static 
14               {
15                   int ss=10;
16                   price=100;
17                   System.out.println("the price is:"+price);
18               }
19             
20               {
21                   price++;
22               }
23               
24               public Product(int id, String name,int price) {
25                     this.id = id;
26                     this.name = name;
27                     this.price=price;
28                 }
29               public String toString() 
30               {
31                   return id+","+name+","+price;
32               }
33               {
34                   name="Sharplee";
35               }
36               public void print() 
37               {
38                   System.out.println(toString());
39               }
40               
41               {
42                   System.out.println("id is "+id);
43                   System.out.println("name is "+name);
44                   System.out.println(price);
45               }
46               
47 }
48 
49 public class TestProduct
50 {
51     public static void main(String[]args) 
52     {
53         Product p=null;
54         Product t=new Product();
55         Product c=new Product();
56     
57         
58     }
59 }
View Code

对象创建的执行顺序

总结:对象创建首先进行类实例变量以及类代码块的初始化。接着是类的父类的对象代码块执行,类的父类构造函数执行,最后执行类中代码块以及类的构造函数。

原文地址:https://www.cnblogs.com/Hackerman/p/7596883.html