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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:38:48+00:00 2026-05-16T00:38:48+00:00

I was reading some code found on the web and fell on these lines

  • 0

I was reading some code found on the web and fell on these lines (java):

private List<String> values;

[...]

for (final String str : values) {
   length += context.strlen(str);
}

What is the advantage of declaring a variable final in a for loop ? I thought the variable specified in the for loop was already read-only (e.g. can’t assign something to ‘str’ in the above example).

Thx

  • 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-16T00:38:49+00:00Added an answer on May 16, 2026 at 12:38 am

    What is the advantage of declaring a variable final in a for loop ?

    Not much in a small piece of code, but, if it would help to avoid changing the reference while looping.

    For instance:

    for( String s : values ) {
         computeSomething( s );
          ... a dozen of lines here... 
         s = s.trim();// or worst s = getOtherString();
         ... another dozen of line 
         otherComputation( s );
     }
    

    If you don’t use final, the last line anotherComputation can potentially use a different value than the one was defined in the iteration and subtle bugs may be introduced, while reading other code the maintainer will try to figure out how come that method is failing with the correct values.

    For a 5 – 15 lines long for this is easy to spot, but for larger segments of code, believe me, is much harder.

    Using final will prevent value change at compile time.

    ... 
    for( final String s : values ) {
      s = new StringBuilder( s ).reverse().toString();
    }
    

    This code fails at compile time variable s might already have been assigned

    Another advantage could be to allow the variable to be used inside an anonymous inner class:

    for( final String s : value ) {
        doSomething( new InnerClass() {
           void something() {
                 s.length();// accesing s from within the inner class 
           }
        });
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading through some JMockit examples and found this code: final List<OrderItem> actualItems
When reading some FreeBSD source code (See: radix.h lines 158-173), I found variable declarations
I'm reading some source code at https://github.com/plataformatec/devise and found that line of code: class_eval
I am new to vbscript and while reading i found some code as Do
When I reading the code of CodeIgniter,I found some functions written as follows: function
I have been reading over some code lately and came across some lines such
I was just reading some code and found that the person was using arr[-2]
I am reading some source code and I found this statement at the very
Reading some source code, I have found next traits definition: namespace dds { template
I was reading some php code source and found the following: $failed |= is_numeric(

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.