Java中数据字段的初始化顺序 [英] Order of initialization of data fields in java

查看:82
本文介绍了Java中数据字段的初始化顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,数据字段的初始化顺序是什么? Java对数据成员和成员函数遵循的一般规则是什么?

In the code below, what is the order of initialization of data fields? What is the general rule followed by java for data member and member functions?

public class TestClass 
{
  int j=10;
  static int h=5;

  public static void main(String[] args)
  {
     TestClass obj= new TestClass();       
  }
}


推荐答案

在常规:

1)静态字段成员(通常是静态初始化程序)

1) static field members (static initializers in general)

2)非静态字段成员

3)构造函数

不过,您可以使用以下代码段对其进行测试:

However you can test it with a snippet of code like this:

public class TestClass {
    int i = 10;
    static int j = 20;
    public TestClass() {
        // TODO Auto-generated constructor stub
        System.out.println(i);
        i = 20;
        System.out.println(i);
    }
    public static void main(String[] args) {
        new TestClass();
    }
}

引用伟大的 Java思维:

Quoting from the great "Thinking In Java":


在一个类中,初始化顺序由变量在该类中定义的
的顺序确定。变量定义可能分散在整个方法定义之间以及方法定义之间,但是变量会在任何方法可以被调用
甚至构造函数之前进行初始化。
...................................
只有一个存储空间对于静态对象,无论创建了多少个对象。
您不能将static关键字应用于局部变量,因此它仅应用于字段。如果字段是
静态基元,而您没有对其进行初始化,则该字段将获得其类型的标准初始值。如果是
对一个对象的引用,则默认初始化值为null。

Within a class, the order of initialization is determined by the order that the variables are defined within the class. The variable definitions may be scattered throughout and in between method definitions, but the variables are initialized before any methods can be called—even the constructor. ................................... There’s only a single piece of storage for a static, regardless of how many objects are created. You can’t apply the statickeyword to local variables, so it only applies to fields. If a field is a staticprimitive and you don’t initialize it, it gets the standard initial value for its type. If it’s a reference to an object, the default initialization value is null.






要总结创建对象的过程,请考虑一个称为Dog的类:


To summarize the process of creatingan object, consider a class called Dog:


  1. 即使没有显式使用static关键字,构造函数实际上也是
    静态方法。因此,第一次创建Dog类型的对象,或第一次访问
    静态方法或Dog类的静态字段时,Java解释器必须
    找到Dog.class,它通过搜索来完成通过课程路径。

  1. Even though it doesn't explicitly use the static keyword, the constructor is actually a static method. So the first time an object of type Dog is created, or the first time a static method or static field of class Dog is accessed, the Java interpreter must locate Dog.class, which it does by searching through the class path.

在加载Dog.class时(创建一个Class对象,您将在后面学习),所有
的静态初始化程序都将运行。因此,静态初始化仅发生一次,因为
Class对象是第一次加载。

As Dog.class is loaded (creating a Class object, which you’ll learn about later), all of its static initializers are run. Thus, static initialization takes place only once, as the Class object is loaded for the first time.

创建新的Dog()时,首先为Dog对象的构造过程
为堆上的Dog对象分配足够的存储空间。

When you create a new Dog( ), the construction process for a Dog object first allocates enough storage for a Dog object on the heap.

此存储空间被清除为零,从而自动将该Dog
对象中的所有基元设置为默认值(数字为零,布尔值和
char)以及对null的引用。

This storage is wiped to zero, automatically setting all the primitives in that Dog object to their default values (zero for numbers and the equivalent for boolean and char) and the references to null.

执行在字段定义时发生的任何初始化。

Any initializations that occur at the point of field definition are executed.

构造函数被执行。实际上可能涉及大量的活动,尤其是在涉及继承的情况下。

Constructors are executed.This might actually involve a fair amount of activity, especially when inheritance is involved.

这篇关于Java中数据字段的初始化顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆