开发者

Use of final variable in signature of Java SwingWorker method

开发者 https://www.devze.com 2023-04-02 22:03 出处:网络
I have a method that uses a SwingWorker.Part of the code is shown below: public class mySocket { public void writePacket(final String packet) {
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 differen开发者_运维知识库t values, I need to reinstantiate the class for each new value of the variable packet ?

Thanks in advance for your help.

Elliott


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:

  • Cannot refer to a non-final variable inside an inner class defined in a different method
  • Cannot refer/modify non-final variable in an innerclass
  • access to variable within inner class in java


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号