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 8685525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:35:51+00:00 2026-06-12T22:35:51+00:00

This has been solved by Robin . Thanks Robin! The idea behind what I

  • 0

This has been solved by Robin. Thanks Robin!

The idea behind what I want to do is make a timer that performs an action every X seconds, but X has to change between uses.

Right now I’m doing it like this:

 try {
        final FileWriter fstream = new FileWriter("timetest.log");
        final BufferedWriter out = new BufferedWriter(fstream);

        ActionListener task_performer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                critical_requests[0]++;
                try {
                    System.out.println("DEBUG: Critical Section requests: " + critical_requests[0] + "\n");
                    out.write("Critical Section request:\t" + critical_requests[0] + "\n");
                } catch (IOException e) {
                    System.out.println(e.getMessage() + "\n");
                }
                ((Timer)evt.getSource()).setDelay( 150 + (rand.nextInt(10) * time_unit ));
            }
        };
        new Timer(wait_delay, task_performer).start();
        System.out.println("Entering while loop\n");
        while(true) {
            if(critical_requests[0] >= 60){
                try {
                    out.close();
                } catch (IOException e) {
                    System.out.println("Close failed for some reason:\t" + e.getMessage() + "\n");
                    System.exit(-1);
                }
                System.exit(0);
            }
            //System.out.println("" + critical_requests[0] + "\n");    // Debug
            critical_requests[0] = critical_requests[0];    // Java is an insane language and it requires me to have this line here
        }
    } catch (IOException e) {
                System.out.println(e.getMessage());
                System.exit(-1);
    }

The error I get is:

local variable is accessed from within inner class; needs to be declared final

I tried making some of them final, but then I can’t change the values inside of the listener. Plus some of the variables don’t make sense to make final (the BufferedWriter out, rand).

All 5 compiler errors are:
local variable rand is accessed from within inner class; needs to be declared final
one each for out, rand, and wait_delay, and two for critical_requests.

How can I reconcile this?

  • 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-06-12T22:35:53+00:00Added an answer on June 12, 2026 at 10:35 pm

    Looking at your code the problem is the wait_delay and the critical_requests variable.

    1. There is no real need to change it inside the ActionListener. You will have to set it on the Timer again before it has any effect on the delay of the Timer
    2. Creating a new Timer object each time you change the wait_delay variable will result in a lot of Timer instances, which each keep running as a Timer repeats by default. Only if you have called setRepeats( false ) they will actually stop. Looking at your comment (which should have been part of the question), you want to update the delay of the Timer
    3. If you really have to change the critical_requests variable inside the ActionListener, you can store it in a final array

    So I would suggest to change your code to something like

    final int[] critical_requests = new int[]{ 0 };
    final Outputstream out = ...;
    ActionListener task_performer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                critical_requests[0] = critical_requests[0] + 1;
                try {
                    out.write("Critical Section request:\t" + (critical_requests[0]) + "\n");
                    ((Timer)evt.getSource()).setDelay( 10 + (rand.nextInt() % 10) );
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                    System.exit(-1);
                }
            }
    };
    Timer the_one_and_only_timer = new Timer( wait_delay, task_performer );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem has been solved! Thanks a lot to Brad, Denis and junkie! You're
I understand that this has been solved in C/C++, but I am not comfortable
Edit: This has since been solved. Thanks to everyone who helped. Invoking the method
This is probably a variation on something that has been solved dozens of times
This problem has been solved thanks to your suggestions. See the bottom for details.
EDIT: This problem has been solved. See below. Hey all. I'm building an iPhone
Update II Problem Solved but Why? This has been the biggest headache ever. My
this has been an ongoing problem with me, ive been trying to make a
Update: this leak has been solved. If you are getting similar leaks and your
you will think: "This problem has been solved many, many times. So why doesn't

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.