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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:47:45+00:00 2026-05-31T06:47:45+00:00

I have this code: List<Runnable> r = new ArrayList<>(); for(int i = 0; i

  • 0

I have this code:

    List<Runnable> r = new ArrayList<>();
    for(int i = 0; i < 10; i++) {
        r.add(new Runnable() {

            @Override
            public void run() {
                System.out.println(i);
            }
        });
    }

It obviously does not compile because i would need to be final to be used in the anonymous class. But I can’t make it final because it is not. What would you do? A solution is to duplicate it but I thought there might be a better way:

    List<Runnable> r = new ArrayList<>();
    for(int i = 0; i < 10; i++) {
        final int i_final = i;
        r.add(new Runnable() {

            @Override
            public void run() {
                System.out.println(i_final);
            }
        });
    }

EDIT just to make it clear, I used a Runnable here for the sake of the example, the question is really about anonymous classes, which could be anything else.

  • 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-31T06:47:46+00:00Added an answer on May 31, 2026 at 6:47 am

    I think your solution is the simplest way.

    Another option would be to refactor the creation of the inner class into a factory function that does it for you, then your loop itself could be something clean like:

    List<Runnable> r = new ArrayList<>();
    for(int i = 0; i < 10; i++) {
        r.add(generateRunnablePrinter(i));
    }
    

    And the factory function could just declare a final parameter:

    private Runnable generateRunnablePrinter(final int value) {
        return new Runnable() {
           public void run() {
               System.out.println(value);
           }
        };
    }
    

    I prefer this refactored approach because it keeps the code cleaner, is relatively self descriptive and also hides away all the inner class plumbing.

    Random digression: if you consider anonymous inner classes to be equivalent to closures, then generateRunnablePrinter is effectively a higher order function. Who said you can’t do functional programming in Java 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this code: var list = new List<int>(); for(int i=0;i<10;i++) list.Add(i); for(int i=0;i<10;i++)
i have this code: public class Test{ arrayList<String> list = new ArrayList<String>(); String[][] temp_list;
I have this code: FVDTO.setStatus(fail); List<String[]> invalidFields = new ArrayList<String[]>(); Iterator<ConstraintViolation<HazardSubmission>> iterator = cv.iterator();
Lets say I have this code: public void MyMethod(string Data, List<string> InputData) { //I
I have this Java code which is used for JSF pagination: public List<ActiveSessionObj> list(int
recently I have seen this code arrMove = new List<int[]>(4); m_pppiCaseMoveDiagLine = new int[64][][];
I have this code: List<int> list = new List<int>(); for (int i = 1;
I have this code snippet import java.util.ArrayList; import java.util.List; public class AssertTest { public
I have this code public List<CalendarData> GetCalendarData(DateTime day) { List<CalendarData> list = new List<CalendarData>();
Say I have this code - public interface ParentInterface1 { public List<? extends ChildInterface1>

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.