在java类中如何/何时执行括号中的代码?

发布于 2024-12-11 18:02:24 字数 319 浏览 0 评论 0原文

所以我在这里查看这个问题。人们建议您可以将代码放入 java 类中,如下所示

class myClass
{
  int a;

  {
    //insert code here dealing with a
  }
}

括号中的代码如何执行以及何时执行?我可以放在那里的内容是否有任何限制,或者它只是保留用于变量初始化?

谢谢!

So I was looking at this question here. And people were suggesting that you can put code in a java class like so

class myClass
{
  int a;

  {
    //insert code here dealing with a
  }
}

How does the code in the brackets get executed and when does it get executed? Is there any limit to what I can put there or is it just reserved for variable initialization?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南薇 2024-12-18 18:02:24
class X

    Foo x = something;  // a variable initializer

    // an instance initializer
    { 
        statement1 
    }

    ...

    X()
    {
        statement2
    }

构造函数被翻译为

    X()
    {
        super();

        // instance initializers and variable initializers
        // in their textual order
        x = something;
        statement1    
        ...

        // the original constructor body
        statement2
    }
class X

    Foo x = something;  // a variable initializer

    // an instance initializer
    { 
        statement1 
    }

    ...

    X()
    {
        statement2
    }

the constructor is translated to

    X()
    {
        super();

        // instance initializers and variable initializers
        // in their textual order
        x = something;
        statement1    
        ...

        // the original constructor body
        statement2
    }
迟月 2024-12-18 18:02:24

这是一个实例初始化块。我建议阅读这篇文章。它在当前类中的任何构造函数执行之前以及所有超类构造函数完成之后调用。

This is an instance initialization block. I would recommend reading this article. It is called before any constructor in the current class is executed and after all super-class constructors have completed.

〃安静 2024-12-18 18:02:24

您可以编写一个简单的测试来检查不同块的执行顺序:

class MyClass {
        MyClass() {
                System.out.println("construtor");
        }

        static {
                System.out.println("static block");
        }

        {
                System.out.println("initialization block");
        }
}

结果:

static block
initialization block
construtor

You could write a simple test to check the order of execution of different blocks:

class MyClass {
        MyClass() {
                System.out.println("construtor");
        }

        static {
                System.out.println("static block");
        }

        {
                System.out.println("initialization block");
        }
}

Result:

static block
initialization block
construtor
云之铃。 2024-12-18 18:02:24

该代码块针对每个实例执行,即它可以访问实例方法、字段等。

它在该类的构造函数层次结构执行之后但在该类的构造函数之前执行。

因此,您必须小心访问其中的数据。

示例:

class Foo {
  protected String name;

  {
    System.out.println("Foo's name: " + name);  
  } 

  public Foo(String n) {
    name = n; 
    System.out.println("constructing Foo");
  }
}

class Bar extends Foo {
  {
    System.out.println("Bar's name: " + name); 
  }

  public Bar(String n) {
    super(n); 
    System.out.println("constructing Bar");
  }
}

如果您现在使用 new Bar("baz"); 创建一个新的 Bar 实例,您将看到以下输出:

Foo's name: null
constructing Foo
Bar's name: baz
constructing Bar

如您所见,此的执行顺序是:

  1. Foo 初始化块
  2. Foo 构造函数
  3. Bar 初始化块
  4. Bar 构造函数

This code block is executed per instance i.e. it has access to instance methods, fields etc.

It is executed after the constructor hierarchy of that class has been executed but before that class' constructor.

Thus you have to be careful what data you access in there.

Example:

class Foo {
  protected String name;

  {
    System.out.println("Foo's name: " + name);  
  } 

  public Foo(String n) {
    name = n; 
    System.out.println("constructing Foo");
  }
}

class Bar extends Foo {
  {
    System.out.println("Bar's name: " + name); 
  }

  public Bar(String n) {
    super(n); 
    System.out.println("constructing Bar");
  }
}

If you now create a new Bar instance using new Bar("baz");, you would see this output:

Foo's name: null
constructing Foo
Bar's name: baz
constructing Bar

As you can see, the execution order of this is:

  1. Foo initializer block
  2. Foo constructor
  3. Bar initializer block
  4. Bar constructor
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文