Search code examples
javascopefinalswingworker

Use of final variable in signature of Java SwingWorker method


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


Solution

  • 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: