欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

java 实例变量初始化_java学习之实例变量初始化

发布时间:2025/3/21 编程问答 52 豆豆
生活随笔 收集整理的这篇文章主要介绍了 java 实例变量初始化_java学习之实例变量初始化 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

实例变量的初始化方法

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

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

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

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

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

1 packagecom.java.test;2

3

4

5 classProduct {6 private intid;7 privateString name;8

9 publicProduct() {10

11 }12

13 public Product(intid, String name) {14 this.id =id;15 this.name =name;16 }17 publicString toString()18 {19 return id+","+name;20 }21

22 public voidprint()23 {24 System.out.println(toString());25 }26 }27

28 public classTestProduct29 {30 public static voidmain(String[]args)31 {32 Product c=newProduct();33 c.print();34 //结果是0,null

35 Product s=new Product(10,"apple");36 s.print();37 //结果是10,apple

38

39 }40 }

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

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

1 classSomeClass2 {3 static booleanb;4 static byteby;5 static charc;6 static doubled;7 static floatf;8 static inti;9 static longl;10 static shorts;11 staticString st;12 }

初始化结果为

false

0\u00000.0

0.0

0

0

0

null

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

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

1 packagetest;2

3

4

5 classProduct {6 private intid;7 privateString name;8

9 publicProduct() {10

11 }12

13 public Product(intid, String name) {14 this.id =id;15 this.name =name;16 }17 publicString toString()18 {19 return id+","+name;20 }21 {

22 name="Sharplee";

23 }24 public voidprint()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 classTestProduct37 {38 public static voidmain(String[]args)39 {40 Product c=newProduct();//id is0 name isSharplee41 c.print();//0,Sharplee42

43 Product s=new Product(10,"apple");//id is0 name isSharplee44 s.print();//10,apple45

46

47 }48 }

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

类代码块

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

1 packagetest;2

3

4

5 classProduct {6 private static intprice;7 private intid;8 privateString name;9

10 publicProduct() {11

12 }13 static

14 {15 price=100;16 System.out.println("the price is:"+price);17 }18 public Product(int id, String name,intprice) {19 this.id =id;20 this.name =name;21 this.price=price;22 }23 publicString toString()24 {25 return id+","+name+","+price;26 }27 {28 name="Sharplee";29 }30 public voidprint()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 classTestProduct44 {45 public static voidmain(String[]args)46 {47 Product c=newProduct();48 c.print();49

50 Product s=new Product(10,"apple",300);51 s.print();52

53

54

55 }56 }

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

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

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

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

1 packagetest;2

3

4

5 classProduct {6 private static intprice;7 private intid;8 privateString name;9

10 publicProduct() {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,intprice) {25 this.id =id;26 this.name =name;27 this.price=price;28 }29 publicString toString()30 {31 return id+","+name+","+price;32 }33 {34 name="Sharplee";35 }36 public voidprint()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 classTestProduct50 {51 public static voidmain(String[]args)52 {53 Product p=null;54 Product t=newProduct();55 Product c=newProduct();56

57

58 }59 }

View Code

1 packagetest;2

3

4

5 classProduct {6 private static intprice;7 private intid;8 privateString name;9

10 publicProduct() {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,intprice) {25 this.id =id;26 this.name =name;27 this.price=price;28 }29 publicString toString()30 {31 return id+","+name+","+price;32 }33 {34 name="Sharplee";35 }36 public voidprint()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 classTestProduct50 {51 public static voidmain(String[]args)52 {53 Product p=null;54 Product t=newProduct();55 Product c=newProduct();56

57

58 }59 }

View Code

对象创建的执行顺序

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

总结

以上是生活随笔为你收集整理的java 实例变量初始化_java学习之实例变量初始化的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。