Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6570497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:45:49+00:00 2026-05-25T14:45:49+00:00

I have a method that uses a SwingWorker. Part of the code is shown

  • 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;
        }

                ..................

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:45:50+00:00Added an answer on May 25, 2026 at 2:45 pm

    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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method I'm writing that uses reflection to list a class's static
I have a method that uses the params keyword, like so: private void ParamsMethod(params
I have a class that implements IDisposable public class Foo: IDisposable { public void
We have a very simple method that uses findById. public Cart getCart(long cartId) {
I have a public method that uses a local private method to get data
I have a method that uses the varargs feature: void add(Animal ...); Now, instead
I have a utility method that uses Display Templates to generate HTML: public static
I am starting out with unit testing, I have a method that uses the
I have a method set up that uses jquery form for a file upload
I have an application (ASP.NET MVC) that uses a Next New method to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.