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

  • Home
  • SEARCH
  • 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 5960853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:53:00+00:00 2026-05-22T18:53:00+00:00

The usual form the of for each loop is this: for(Foo bar: bars){ bar.doThings();

  • 0

The usual form the of for each loop is this:

for(Foo bar: bars){
    bar.doThings();
}

But if I want to retain bar until after the loop, I can not use the for each loop:

Foo bar = null;
// - Syntax error on token "bar", Identifier expected after this token
for(bar: bars){ 
    if(bar.condition())
        break;
}
bar.doThings();

The for loop gets the syntax error mentioned above.

Why is this?
I’m not interested in workarounds, but just curious to the considerations behind this limitation.

In contrast, with an ordinary for loop, the variable can be declared outside or not at all…

int i = 1;
for(;i<max;i++){
    for(;;){
        // Do things
    }
}
  • 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-22T18:53:01+00:00Added an answer on May 22, 2026 at 6:53 pm

    This is a good question and I would be happy to see some in-depth answer. However, the official documentation says:

    These shortcomings were known by the
    designers, who made a conscious
    decision to go with a clean, simple
    construct that would cover the great
    majority of cases.

    The great majority of cases is the answer for me.

    On a side note, personally, I think that foreach loop in Java is just a nice syntax for a standard iterator loop. So, the compiler creates the iterator for the structure and uses the variable to get the value for current iteration. To ensure that the variable has been initialised you need to declare it for the scope of loop (and I think this prevents the variable from being used somewhere else, e.g. in another thread). Because of that, you cannot use the variable after the loop. But, this is just my opinion and I would be very happy to hear from someone who knows it better. 🙂

    Edit Here is an interesting article about foreach loops in Java.

    Another edit I analysed (with jclasslib the bytecode of these methods:

     private static void testForEach(ArrayList<String> als) {
      for(String s: als)
        System.out.println(s);
     }
    
     private static void testIterator(ArrayList<String> als) {
      for(Iterator<String> is = als.iterator(); is.hasNext();) {
       String s = is.next();
       System.out.println(s);
      } 
     }
    

    Both methods are represented by the same bytecode:

     0 aload_0
     1 invokevirtual #2 <java/util/ArrayList.iterator>
     4 astore_1
     5 aload_1
     6 invokeinterface #3 <java/util/Iterator.hasNext> count 1
    11 ifeq 34 (+23)
    14 aload_1
    15 invokeinterface #4 <java/util/Iterator.next> count 1
    20 checkcast #5 <java/lang/String>
    23 astore_2
    24 getstatic #6 <java/lang/System.out>
    27 aload_2
    28 invokevirtual #7 <java/io/PrintStream.println>
    31 goto 5 (-26)
    34 return
    

    The difference is in line 1, the latter method uses invokevirtual #8. However, both invocations result in calling the same method of Iterator. So, it seems that foreach is nothing more than a syntactic sugar that the compiler just translates into a predefined construct, just as the documentation says. This does not answer the question why it is the way it is. I though this is just interesting and might be worth to know, in the context of the discussion.

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

Sidebar

Related Questions

I want to make preview button as usual form button next to submit button(as
So GET forms make the usual urls like .../search/?q=apple Can you make a form
A .NET Windows form can have a help button on the title bar when
The usual way I do form handling is like this: contact = Contact( name=form.cleaned_data['name'],
I have this form where it submits by pressing enter key. I want it
I have a usual login form consisting of two input fields, one for login,
The usual way to assign color box functionality on a link is like this:
The usual pattern for a singleton class is something like static Foo &getInst() {
As usual, when a tableviewcell is touched, it will be selected. But now i
I have a standard admin change form for an object, with the usual StackedInline

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.