什么叫对象初始化块?什么时候被初始化?

myStock2008upupup 2008-03-06 11:52:55
/**
@version 1.01 2004-02-19
@author Cay Horstmann
*/

import java.util.*;

public class ConstructorTest
{
public static void main(String[] args)
{
// fill the staff array with three Employee objects
Employee[] staff = new Employee[3];

staff[0] = new Employee("Harry", 40000);
staff[1] = new Employee(60000);
staff[2] = new Employee();

// print out information about all Employee objects
for (Employee e : staff)
System.out.println("name=" + e.getName()
+ ",id=" + e.getId()
+ ",salary=" + e.getSalary());
}
}

class Employee
{
// three overloaded constructors
public Employee(String n, double s)
{
name = n;
salary = s;
}

public Employee(double s)
{
// calls the Employee(String, double) constructor
this("Employee #" + nextId, s);
}

// the default constructor
public Employee()
{
// name initialized to ""--see below
// salary not explicitly set--initialized to 0
// id initialized in initialization block
}

public String getName()
{
return name;
}

public double getSalary()
{
return salary;
}

public int getId()
{
return id;
}

private static int nextId;

private int id;
private String name = ""; // instance field initialization
private double salary;

// static initialization block
static
{
Random generator = new Random();
// set nextId to a random number between 0 and 9999
nextId = generator.nextInt(10000);
}

// object initialization block 什么是对象初始化块?什么时候被初始化?
{
id = nextId;
nextId++;
}
}
...全文
278 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
phsyf 2008-03-07
  • 打赏
  • 举报
回复
这个很神奇的
当这个类被初始化的时候,同时这个静态模块就运行了
hmsuccess 2008-03-07
  • 打赏
  • 举报
回复
静态初始化块:使用关键字static定义的代码块。当类装载到系统时执行一次,静态初始化块只能初始化类的静态数据成员。

非静态初始化块:对每个要生成的对象执行一次。可以初始化静态数据成员以及实例数据成员。

一个类可以有多个初始化块,在同种类型情况下,它们将按照在程序中出现的顺序运行,但是有如下规则:

静态初始化块在非静态初始化块之前执行。

静态/非静态初始化块在构造函数之前执行。

当类创建一个对象了的初始顺序:
1. 父类静态成员和静态初始化块 ,按在代码中出现的顺序依次执行
2. 子类静态成员和静态初始化块 ,按在代码中出现的顺序依次执行
3. 父类实例成员和实例初始化块 ,按在代码中出现的顺序依次执行
4. 父类构造方法
5. 子类实例成员和实例初始化块 ,按在代码中出现的顺序依次执行
6. 子类构造方法

Fenglee2008 2008-03-07
  • 打赏
  • 举报
回复
static
{
Random generator = new Random();
// set nextId to a random number between 0 and 9999
nextId = generator.nextInt(10000);
}
上面这段是类被加载时调用

// object initialization block 什么是对象初始化块?什么时候被初始化?
{
id = nextId;
nextId++;
}
实例化该类的对象时调用
sky_ccy 2008-03-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 a1405 的回复:]
想知道到那段代码在什么时候被调用很简单,你只要在那段代码前加上System.out.println("xxx方法被调用");就行了!不用死记硬背。
[/Quote]
很经典呀,呵呵,,结合书看一下,,呵呵
a1405 2008-03-07
  • 打赏
  • 举报
回复
想知道到那段代码在什么时候被调用很简单,你只要在那段代码前加上System.out.println("xxx方法被调用");就行了!不用死记硬背。
myStock2008upupup 2008-03-07
  • 打赏
  • 举报
回复
/** 
@version 1.01 2004-02-19
@author Cay Horstmann
*/

import java.util.*;

public class ConstructorTest
{
public static void main(String[] args)
{
// fill the staff array with three Employee objects
Employee[] staff = new Employee[3];

staff[0] = new Employee("Harry", 40000);
staff[1] = new Employee(60000);
staff[2] = new Employee();

// print out information about all Employee objects
for (Employee e : staff)
System.out.println("name=" + e.getName()
+ ",id=" + e.getId()
+ ",salary=" + e.getSalary());
}
}

class Employee
{
// three overloaded constructors
public Employee(String n, double s)
{
name = n;
salary = s;
}

public Employee(double s)
{
// calls the Employee(String, double) constructor
this("Employee #" + nextId, s);
}

// the default constructor
public Employee()
{
// name initialized to ""--see below
// salary not explicitly set--initialized to 0
// id initialized in initialization block
}

public String getName()
{
return name;
}

public double getSalary()
{
return salary;
}

public int getId()
{
return id;
}

private static int nextId;

private int id;
private String name = ""; // instance field initialization
private double salary;

// static initialization block
static
{
Random generator = new Random();
// set nextId to a random number between 0 and 9999
nextId = generator.nextInt(10000);
}

// object initialization block 什么是对象初始化块?什么时候被初始化?
{
id = nextId;
nextId++;
}
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧