在Java中,为什么有一个没有关键字、只有大括号的代码块
我正在重构一些继承的代码,但被设计决策所困扰,无法找出合适的术语来谷歌搜索。我的前任会使用这样的块:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它是一个初始化块。 (与静态初始化程序块相关)请参阅本页上的初始化实例成员:
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.
它称为初始化块。
It's called an initializer block.
您的前任仍在学习。
这是您可能得到的最好解释。也许在某个时间点需要像这样拆分代码。很难说。代码当然应该这样写:
至于初始化块,其目的已由此处的其他答案给出。我提出我的答案是为了尝试回答您所要求的“为什么”。不幸的是,要获得真正的答案,您必须询问您的前任。
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:
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.
范围。块中声明的任何变量都会在块之后超出范围。将变量的作用域保持在最小范围是很有用的。
另外,如果定义匿名内部类,则可以在构造函数中使用此语法。
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.