简体   繁体   English

最终变量赋值:在声明中还是在构造函数中?

[英]final variable assignment: at declaration or in constructor?

First of all this is NOT an exact duplicate of Initialize final variable before constructor in Java .首先,这不是Java 中在构造函数之前初始化最终变量的完全副本。 It is probably related, but there aren't any answers that satisfy me.这可能是相关的,但没有任何令我满意的答案。

My problem is about final variables in Swing GUI's.我的问题是关于 Swing GUI 中的最终变量。 It's about custom Action s in particular.特别是关于自定义Action的。 I have a number of final variables and a number of static final variables.我有一些final变量和一些static final变量。

The question is: if the variable is actually a constant, what is better: initialise them at construction-time, or initialise them at declaration?问题是:如果变量实际上是一个常量,那么哪个更好:在构造时初始化它们,还是在声明时初始化它们?

The answers on the question I mentionned above generally point towards making the variable static as soon as you are able to assign it when you declare it.我上面提到的问题的答案通常指向变量static ,只要你在声明它时能够分配它。 That doesn't really make sense to me, as the variables aren't used in a static context.这对我来说真的没有意义,因为变量没有在 static 上下文中使用。 I have a couple of Images that my form uses like icons, I made those static because an Image simply is a static thing unless your application modifies them.我有几个我的表单使用的图像,如图标,我制作了这些 static 因为图像只是一个 static 的东西,除非你的应用程序修改它们。 That makes sense.这就说得通了。

On the other hand, the Action s are new instances of a custom inner class. Very technically they are static too, but it just feels different.另一方面, Action是自定义内部 class 的新实例。从技术上讲,它们也是 static,但感觉不同。 They simply mustn't be available in static context imo.它们根本不能在 static 上下文 imo 中可用。 So do I put:我也这么说:

private final CustomAction customAction = new CustomAction();

Or do I initialise it in the constructor?还是在构造函数中初始化它? Which is better?哪个更好? Or am I thinking the wrong way about static ?还是我对static的想法有误?

If the field is a constant, make it a static final member of the class,如果该字段是常量,则使其成为static final成员 static,

public class Foo{
    public static final int BAR = ...;
}

Otherwise, initialize the field in the constructor.否则,在构造函数中初始化字段。

Initialize your constant variables at declaration: it is more readable.在声明时初始化常量变量:它更具可读性。 Make it static if it does not make any sense to put different values into it for different instances of the class, that is, if it is a class level variable, not an instance level.如果为 class 的不同实例将不同的值放入它没有任何意义,也就是说,如果它是 class 级别变量,而不是实例级别,则将其设置为 static。

I think you're on the right track with not making it static, because it sounds like your CustomAction objects are truly custom to the instance of the GUI that creates them in its constructor.我认为您在正确的轨道上没有将其设为 static,因为听起来您的CustomAction对象是真正自定义到在其构造函数中创建它们的 GUI 实例。 I think whether you initialize it in the constructor or not depends on whether your constructor could initialize a CustomAction differently based on the constructor's input arguments.我认为您是否在构造函数中初始化它取决于您的构造函数是否可以根据构造函数的输入 arguments 以不同方式初始化CustomAction

Where static versus non-static is concerned... a good rule of thumb is, if a variable is going to remain constant across all instances of a particular object type, then that variable should be static .其中static与非静态有关......一个好的经验法则是,如果一个变量要在特定 object 类型的所有实例中保持不变,那么该变量应该是static This saves memory over the runtime of your program and also saves CPU time when each object instance is constructed, since that constant won't have to be initialized every time you create a new instance of your Object. On the other hand, if a variable is going to remain constant for a particular instance of an Object, but may be different from instance to instance, then it shouldn't be static.这在程序运行时节省了 memory ,并且在构造每个 object 实例时也节省了 CPU 时间,因为每次创建 Object 的新实例时都不必初始化该常量。另一方面,如果一个变量对于 Object 的特定实例将保持不变,但可能因实例而异,那么它不应该是 static。

Finally (pun intended), final should be used whenever you don't want a primitive value or reference to an Object to ever change.最后(双关语意),只要您不希望原始值或对 Object 的引用发生变化,就应该使用final The static or non-static context doesn't really influence whether a variable should be final , it's strictly final because the developer doesn't ever want to change that variable. static 或非静态上下文并不会真正影响变量是否应该是final ,它是严格的final因为开发人员永远不想更改该变量。 Its static context depends solely on how the developer wants it to be accessed.它的 static 上下文完全取决于开发人员希望如何访问它。

For a fast application startup and program parts the user possibly does not visit (About dialog), static is not good.对于快速的应用程序启动和用户可能不访问的程序部分(关于对话框),static 不好。 In general static is not very liked as you did find out.一般来说 static 不是很喜欢,因为你确实发现了。 There are some reasons, but nothing very convincing.有一些原因,但没有什么是非常有说服力的。 But sometimes it is a anti-pattern or a sign of it.但有时它是一种反模式或它的标志。

Still in your case I would refrain from static images.仍然在你的情况下,我会避免使用 static 图像。 By the way resources are cached internally.顺便说一句,资源是在内部缓存的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM