在 Java SwingWorker 方法的签名中使用 Final 变量

发布于 2024-12-03 03:08:26 字数 1182 浏览 1 评论 0原文

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 是一个内部类。这引发了以下关于如何以及何时为该变量赋值的问题:

  1. Final 对我来说意味着该变量只能被赋值一次。但因为声明是方法签名的一部分,所以我不知道这在这种情况下意味着什么。
  2. 我总是假设最终变量在实例化类时被赋值,但在这里它是在调用方法时被赋值的。传递给该方法的变量是否也需要声明为 Final 并在类的构造函数中分配才能正常工作?
  3. 我是否可以使用不同的数据包值多次调用该方法,或者因为变量被声明为最终变量,这是否意味着如果我想使用不同的值调用该方法,我需要为变量的每个新值重新实例化该类< 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:

  1. 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.
  2. 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?
  3. 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 技术交流群。

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

发布评论

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

评论(4

浅笑依然 2024-12-10 03:08:26

1。 Final 对我来说意味着变量只能被赋值一次。但因为声明是方法签名的一部分,所以我不知道这在这种情况下意味着什么。

它只是意味着你不能为变量重新分配新值数据包

2。我总是假设最终变量在实例化类时被赋值,但在这里它是在调用方法时被赋值的。传递给该方法的变量是否也需要声明为 Final 并在类的构造函数中分配才能正常工作?

您也可以拥有本地 Final 变量。适用相同的规则,但它们与构造函数/ this 引用无关。

3。我是否可以使用不同的数据包值多次调用该方法,或者因为变量被声明为最终变量,这是否意味着如果我想使用不同的值调用该方法,我需要为变量数据包的每个新值重新实例化该类?

您可以使用不同的值多次调用该方法。 packet 变量就像一个局部变量。不,您不需要为每个值/调用重新实例化该类。

进一步阅读:

1. 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.

It just means that you can't reassign a new value to the variable packet.

2. 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?

You can have local final variables as well. Same rules applies, but they are unrelated to constructor / this reference.

3. 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 ?

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:

素染倾城色 2024-12-10 03:08:26

内部类只能引用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.

瑕疵 2024-12-10 03:08:26

Final 对我来说意味着变量只能被赋值
一次。但由于该声明是签名的一部分
方法,我不知道这在这种情况下意味着什么。

这意味着同样的事情,你不能重新分配变量。

// this compiles
public void foo(String bar){
   bar = "xyz";
}

// this doesn't
public void foo(final String bar){
   bar = "xyz";
}

我总是假设最终变量在以下情况下被赋值
该类已实例化,但此处是在调用方法时对其进行赋值。

不,局部变量也可以是final,参数也可以是final。它们在每个范围(块或方法级别)分配一次。

传递给方法的变量是否也需要
声明为final并在类的构造函数中分配给this
工作?

不,这完全无关紧要

我可以使用不同的数据包值多次调用该方法吗
或者因为该变量被声明为最终变量,这是否意味着如果我
想要调用具有不同值的方法,我需要重新实例化
变量包的每个新值的类?

不,请参阅上文。对于每个方法调用来说,它都是一个新变量。

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.

It means the same thing, you can't re-assign the variable.

// this compiles
public void foo(String bar){
   bar = "xyz";
}

// this doesn't
public void foo(final String bar){
   bar = "xyz";
}

I always assumed that final variables were assigned values when
the class was instantiated but here it is assigned when the method is called.

No, local variables can also be final, and parameters too. They are assigned once per scope (block or method level).

Does the variable passed to the method also need to be
declared final and assigned in the class's constructor for this to
work?

No, that's totally irrelevant

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 ?

No, see above. It's a new variable for each method call.

愿与i 2024-12-10 03:08:26

最终对我来说意味着变量只能被赋值一次。但因为声明是方法签名的一部分,所以我不知道这在这种情况下意味着什么。

同样的事情。这意味着您无法为包含该参数的局部变量重新分配新值(无论如何,这被认为是不好的做法)。

我总是假设最终变量在类实例化时被赋值,但这里它是在调用方法时赋值

第一部分对于最终实例变量来说是正确的,而不是局部变量。

但想法是相同的:需要为它们分配一个值,并且只能发生一次。

我可以使用不同的数据包值多次调用该方法吗?或者因为该变量被声明为最终变量,这是否意味着如果我想使用不同的值调用该方法,我需要为每个新值重新实例化该类变量包?

您可以多次调用它。声明为final的参数对方法的代码调用没有影响(仅对其实现有影响)。

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.

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).

I always assumed that final variables were assigned values when the class was instantiated but here it is assigned when the method is called

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.

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 ?

You can call it many times. The parameter being declared final has no impact to code calling of the method (just for its implementation).

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