在Java中,为什么有一个没有关键字、只有大括号的代码块

发布于 2024-10-07 22:08:27 字数 526 浏览 2 评论 0原文

我正在重构一些继承的代码,但被设计决策所困扰,无法找出合适的术语来谷歌搜索。我的前任会使用这样的块:

public class ChildClass extends ParentClass {
    {
        inheritedVar = "someVal";
    }

    public ChildClass(){ /* constructor exists */ }
    // rest of code
}

声明没有关键字的代码块有什么意义?我不相信它的行为不像静态块。它是构造函数中设置的替代方法吗?如果使用工厂(在本例中不是),这会产生一些影响吗?我在这里找到了一个相关的线程这发生在C 但推理(作用域和变量声明)似乎与 Java 无关。

任何关于“为什么”的想法或想法将不胜感激。重构这个很容易,我现在只是很好奇。

I'm re-factoring some inherited code, but was stumped by the design decision and can't figure out the proper terms to google this. My predecessor would use blocks like this one:

public class ChildClass extends ParentClass {
    {
        inheritedVar = "someVal";
    }

    public ChildClass(){ /* constructor exists */ }
    // rest of code
}

What is the point of declaring a block of code with no keyword? It doesn't behave like a static block, I don't believe. Is it an alternative to setting in the constructor? Would this have some effect if a factory was being used (which in this case it's not)? I found a related thread here on this happening in C but the reasoning (scope & variable declaration) didn't seem relevant to Java.

Any thoughts or ideas on the "why" of this would be appreciated. It's easy enough to re-factor this, I'm just curious at this point.

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

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

发布评论

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

评论(4

旧人哭 2024-10-14 22:08:27

它是一个初始化块。 (与静态初始化程序块相关)请参阅本页上的初始化实例成员:

http: //download.oracle.com/javase/tutorial/java/javaOO/initial.html

它是构造函数的替代方案。当提供多个重载构造函数作为共享代码的方式时,您可以使用它。

然而,就我个人而言,我发现让构造函数调用命名初始化方法而不是依赖匿名代码块更清晰。尽管如此,编译器确实将初始化块复制到幕后的所有构造函数,您可能会认为性能有所提高,类似于内联方法声明。

It is an initializer block. (Related to static initializer block) See Initializing Instance Members on this page:

http://download.oracle.com/javase/tutorial/java/javaOO/initial.html

It is an alternative to a constructor. You could use it when providing multiple, overloaded constructors as a way to share code.

Personally, however, I find it much clearer to have the constructor call a named initializer method rather than rely on the anonymous code block. Although, the compiler does copy the initializer block to all constructors behind the scenes and you could argue that there is a performance increase similar to inline'ing a method declaration.

会发光的星星闪亮亮i 2024-10-14 22:08:27

它称为初始化块

实例变量的初始化块看起来就像静态初始化块,但没有 static 关键字:

<前><代码> {
// 初始化所需的任何代码都放在这里
}

Java 编译器将初始化块复制到每个构造函数中。因此,这种方法可用于在多个构造函数之间共享代码块。

It's called an initializer block.

Initializer blocks for instance variables look just like static initializer blocks, but without the static keyword:

    {
        // whatever code is needed for initialization goes here
    }

The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.

醉生梦死 2024-10-14 22:08:27

您的前任仍在学习。

这是您可能得到的最好解释。也许在某个时间点需要像这样拆分代码。很难说。代码当然应该这样写:

public class ChildClass extends ParentClass {
    public ChildClass() {
        inheritedVar = "someVal";
    }
    // rest of code
}

至于初始化块,其目的已由此处的其他答案给出。我提出我的答案是为了尝试回答您所要求的“为什么”。不幸的是,要获得真正的答案,您必须询问您的前任。

Your predecessor was still learning.

That's the best explanation you're likely to get. Perhaps at one point in time there was a need to have the code split up like this. It's hard to say. The code should certainly be written like this instead:

public class ChildClass extends ParentClass {
    public ChildClass() {
        inheritedVar = "someVal";
    }
    // rest of code
}

As for the initializer block, its purpose has been given by the other answers here. I threw my answer in as an attempt to answer the "why", which you requested. Unfortunately, for the real answer, you would have to ask your predecessor.

好久不见√ 2024-10-14 22:08:27

范围。块中声明的任何变量都会在块之后超出范围。将变量的作用域保持在最小范围是很有用的。

另外,如果定义匿名内部类,则可以在构造函数中使用此语法。

Scope. Any variables declared in the block go out of scope after the block. It's useful to keep variables scoped minimally.

Also, if you define an anonymous inner class, you use this syntax for the constructor.

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