实例变量的初始化方法

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

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

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

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

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

 package com.java.test;

 class Product {
private int id;
private String name; public Product() { } public Product(int id, String name) {
this.id = id;
this.name = name;
}
public String toString()
{
return id+","+name;
} public void print()
{
System.out.println(toString());
}
} public class TestProduct
{
public static void main(String[]args)
{
Product c=new Product();
c.print();
//结果是0,null
Product s=new Product(10,"apple");
s.print();
//结果是10,apple }
}

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

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

 class SomeClass
{
static boolean b;
static byte by;
static char c;
static double d;
static float f;
static int i;
static long l;
static short s;
static String st;
}

初始化结果为

false
0
\u0000
0.0
0.0
0
0
0
null

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

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

 package test;

 class Product {
private int id;
private String name; public Product() { } public Product(int id, String name) {
this.id = id;
this.name = name;
}
public String toString()
{
return id+","+name;
}
21 {
22 name="Sharplee";
23 }
public void print()
{
System.out.println(toString());
} 29 {
30 System.out.println("id is "+id);
31 System.out.println("name is "+name);
32 } } public class TestProduct
{
public static void main(String[]args)
{
Product c=new Product();//id is0 name isSharplee
c.print();//0,Sharplee Product s=new Product(10,"apple");//id is0 name isSharplee
s.print();//10,apple }
}

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

类代码块

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

 package test;

 class Product {
private static int price;
private int id;
private String name; public Product() { }
static
{
price=100;
System.out.println("the price is:"+price);
}
public Product(int id, String name,int price) {
this.id = id;
this.name = name;
this.price=price;
}
public String toString()
{
return id+","+name+","+price;
}
{
name="Sharplee";
}
public void print()
{
System.out.println(toString());
} {
System.out.println("id is "+id);
System.out.println("name is "+name);
System.out.println(price);
} } public class TestProduct
{
public static void main(String[]args)
{
Product c=new Product();
c.print(); Product s=new Product(10,"apple",300);
s.print(); }
}

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

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

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

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

 package test;

 class Product {
private static int price;
private int id;
private String name; public Product() { }
static
{

price=100;
System.out.println("the price is:"+price);
}
static
{
price++;
} public Product(int id, String name,int price) {
this.id = id;
this.name = name;
this.price=price;
}
public String toString()
{
return id+","+name+","+price;
}
{
name="Sharplee";
}
public void print()
{
System.out.println(toString());
} {
System.out.println("id is "+id);
System.out.println("name is "+name);
System.out.println(price);
} } public class TestProduct
{
public static void main(String[]args)
{
Product p=null;
Product t=new Product();
Product c=new Product(); }
}
 package test;

 class Product {
private static int price;
private int id;
private String name; public Product() { }
static
{
int ss=10;
price=100;
System.out.println("the price is:"+price);
} {
price++;
} public Product(int id, String name,int price) {
this.id = id;
this.name = name;
this.price=price;
}
public String toString()
{
return id+","+name+","+price;
}
{
name="Sharplee";
}
public void print()
{
System.out.println(toString());
} {
System.out.println("id is "+id);
System.out.println("name is "+name);
System.out.println(price);
} } public class TestProduct
{
public static void main(String[]args)
{
Product p=null;
Product t=new Product();
Product c=new Product(); }
}

对象创建的执行顺序

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

java学习之实例变量初始化的更多相关文章

  1. Java 类的实例变量初始化的过程 静态块、非静态块、构造函数的加载顺序

    先看一道Java面试题: public class Baset { private String baseName = "base"; // 构造方法 public Baset() ...

  2. Java实例变量初始化

    由一道面试题所想到的--Java实例变量初始化 时间:2015-10-07 16:08:38      阅读:23      评论:0      收藏:0      [点我收藏+] 标签:java   ...

  3. java静态类、静态方法、静态代码块,静态变量及实例方法,实例变量初始化顺序及内存管理,机制

    1.当一个类被第一次使用时,它需要被类加载器加载,而加载过程涉及以下两点: (1)在加载一个类时,如果它的父类还未被加载,那么其父类必须先被加载: (2)当类加载到内存之后,按照在代码中的出现顺序执行 ...

  4. Java类变量、实例变量的初始化顺序

    题目: public class InitTest{ public static int k = 0; public static InitTest t1 = new InitTest("t ...

  5. Java构造方法、成员变量初始化以及静态成员变量初始化三者的先后顺序是什么样的?

    [Java笔试真题]:构造方法.成员变量初始化以及静态成员变量初始化三者的先后顺序是什么样的? [解答]:当类第一次被加载的时候,静态变量会首先初始化,接着编译器会把实例变量初始化为默认值,然后执行构 ...

  6. Java类、实例的初始化顺序

    今晚是阿里巴巴 2013 校园招聘的杭州站笔试.下午匆忙看了两张历年试卷,去现场打了瓶酱油. 题目总体考察点偏基础,倒数第二题(Java 附加题)比较有趣,考察了 Java 初始化机制的细节,在此摘录 ...

  7. Java类变量和成员变量初始化过程

    一.类的初始化 对于类的初始化:类的初始化一般只初始化一次,类的初始化主要是初始化静态成员变量. 类的编译决定了类的初始化过程. 编译器生成的class文件主要对定义在源文件中的类进行了如下的更改: ...

  8. 0020 Java学习笔记-面向对象-变量

    变量分为哪些 成员变量:类里面,方法外面定义的变量 实例变量:没有用static修饰的变量,属于对象:存在期:创建实例-销毁实例:作用域:与该实例的生存范围相同 类变量:用static修饰的变量,属于 ...

  9. oc实例变量初始化方法

    1 使用实例setter方法 默认初始化方法 + setName:xxx setAge:xxx 2 使用实例功能类方法,默认初始化方法 + setName:xxx age:xxx3 使用实例初始化方法 ...

随机推荐

  1. Spring定时器XML配置

    spring-task.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...

  2. JavaScript逻辑and、or、not运算符详解

    一.AND详解: 在JavaScript中,逻辑 AND 运算符用双和号(&&)表示. 需要说明的是:逻辑AND运算的运算数可以是任何类型的,不止是Boolean值,如果某个运算数不是 ...

  3. Sqlite-SQLiteHelper类,操作SQLite数据库

    using System; using System.Data; using System.Text.RegularExpressions; using System.Xml; using Syste ...

  4. CPU五级流水线project(带Hazard)

    project简单介绍: 计算机组成原理课程Project--五级流水线hazard处理 思路说明: CPU架构图: CPU指令集: 代码在这里:cpu_hazard

  5. 一些C++11语言新特性 - Range-Based for Loops

    1. Range-Based for Loops for ( decl : coll ) { statement} eg: , , , , , , , } ) { std::cout << ...

  6. system返回值校验

    int xsystem(const char *cmd){    int err; err = system(cmd); if (err == -1) {    fprintf(stderr, &qu ...

  7. Androidclient性能參数监控

    背景: 在做androidclient測试的时候.有时候须要监控cpu/mem/电量消耗/界面载入时间/流量等等指标. 于是俺们就上下求索,网友告诉我两个方案:AnotherMonitor和Emmag ...

  8. TWaver版3D化学元素周期表

    非常早就有人做3D网页版的化学元素周期表了.酷炫效果和新奇技巧一度被众多粉丝奉为神明,争相研究和效仿.甚至有人放弃一切扑向这颗蜡烛.不由总是想到那个OPPO广告女主角拽拽的歧视道:"辞职去旅 ...

  9. 19-spring学习-springMVC环境配置

    新建一共环境,添加spring支持,就可以开发springMVC了. 既然是springMVC,就必须为其定义相关配置. 1,springMVC所有配置都需要在applicationContext.x ...

  10. Android Studio 怎样打开两个项目?

    欢迎转载: 请注明 原创Url