在 Java SwingWorker 方法的签名中使用 Final 变量
I have a method that uses a SwingWorker. Part of the code is shown below:
public class mySocket {
public void writePacket(final String packet) {
// schedules execution on the single thread of the executor (so only one
// background operation can happen at once)
//
executor.execute(new SwingWorker<String, Void>() {
@Override
protected String doInBackground() throws Exception {
// called on a background thread
System.out.println("writing out this packet->" + packet + "
<-");
out.println(packet);
String thePacket = readPacket();
return thePacket;
}
..................
我的问题是关于 writePacket 方法的签名。变量 packet 必须声明为final,否则编译器会报错,因为SwingWorker 是一个内部类。这引发了以下关于如何以及何时为该变量赋值的问题:
- Final 对我来说意味着该变量只能被赋值一次。但因为声明是方法签名的一部分,所以我不知道这在这种情况下意味着什么。
- 我总是假设最终变量在实例化类时被赋值,但在这里它是在调用方法时被赋值的。传递给该方法的变量是否也需要声明为 Final 并在类的构造函数中分配才能正常工作?
- 我是否可以使用不同的数据包值多次调用该方法,或者因为变量被声明为最终变量,这是否意味着如果我想使用不同的值调用该方法,我需要为变量的每个新值重新实例化该类< b>数据包?
预先感谢您的帮助。
埃利奥特
I have a method that uses a SwingWorker. Part of the code is shown below:
public class mySocket {
public void writePacket(final String packet) {
// schedules execution on the single thread of the executor (so only one
// background operation can happen at once)
//
executor.execute(new SwingWorker<String, Void>() {
@Override
protected String doInBackground() throws Exception {
// called on a background thread
System.out.println("writing out this packet->" + packet + "
<-");
out.println(packet);
String thePacket = readPacket();
return thePacket;
}
..................
My questions are about the signature of the writePacket method. The variable packet must be declared final or the compiler complains because SwingWorker is an inner class. This raises the following questions about how and when I can assign values to this variable:
- Final to me means that the variable can only be assigned a value once. But because the declaration is part of the signature of a method, I don't know what this means in this context.
- I always assumed that final variables were assigned values when the class was instantiated but here it is assigned when the method is called. Does the variable passed to the method also need to be declared final and assigned in the class's constructor for this to work?
- Can I call the method more than once with different values for packet or because the variable is declared final, does this mean that if I want to call the method with different values, I need to reinstantiate the class for each new value of the variable packet?
Thanks in advance for your help.
Elliott
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它只是意味着你不能为变量重新分配新值
数据包
。您也可以拥有本地 Final 变量。适用相同的规则,但它们与构造函数/ this 引用无关。
您可以使用不同的值多次调用该方法。
packet
变量就像一个局部变量。不,您不需要为每个值/调用重新实例化该类。进一步阅读:
It just means that you can't reassign a new value to the variable
packet
.You can have local final variables as well. Same rules applies, but they are unrelated to constructor /
this
reference.You can call the method several times with different values. The
packet
variable is like a local variable. and No, you don't need to reinstantiate the class for each value / call.Further SO reading:
内部类只能引用final变量,因为所需的变量实际上是作为参数传递给内部类ctor的。如果变量不是最终变量,并且在创建内部类后发生更改,则它将不再保留当前变量,而是先前的变量。为了避免这种奇怪的情况并让事情变得简单,变量/参数必须是最终的,以便内部类读取。
The inner class can only reference final variables because the variable that is wantd is actually passed to the inner class ctor as a parameter. If the variable was not final, and changed after the inner class was created, it would no longer be holding onto the current but previous variable. To avoid such strangeness and to keep things simple, variables/parameters must be final for an inner class to read.
这意味着同样的事情,你不能重新分配变量。
不,局部变量也可以是final,参数也可以是final。它们在每个范围(块或方法级别)分配一次。
不,这完全无关紧要
不,请参阅上文。对于每个方法调用来说,它都是一个新变量。
It means the same thing, you can't re-assign the variable.
No, local variables can also be final, and parameters too. They are assigned once per scope (block or method level).
No, that's totally irrelevant
No, see above. It's a new variable for each method call.
同样的事情。这意味着您无法为包含该参数的局部变量重新分配新值(无论如何,这被认为是不好的做法)。
第一部分对于最终实例变量来说是正确的,而不是局部变量。
但想法是相同的:需要为它们分配一个值,并且只能发生一次。
您可以多次调用它。声明为final的参数对方法的代码调用没有影响(仅对其实现有影响)。
Same thing. It means you cannot re-assign a new value to the local variable that contains the parameter (that is considered bad practice anyway).
That first part is true for final instance variables, not for local variables.
But the idea is the same: They need to be assigned a value and it can happen only once.
You can call it many times. The parameter being declared final has no impact to code calling of the method (just for its implementation).