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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:23:21+00:00 2026-05-28T18:23:21+00:00

Found interesting thing while compile the following piece of code: 1 class A {

  • 0

Found interesting thing while compile the following piece of code:

1    class A {
2        
3     private B line;
4   
5     public void foo() {
6       for (Integer line : line.getElements()) {
7    
8       }
9     }
10    }
11    
12    class B {
13    
14      List<Integer> getElements() {
15         return null; // doesn't matter
16      }
17    }

Pay attention to line 6: we see that variable name is equal to field name. IntelliJ Idea 11 ignores this and think there is no trouble here. But java compiler tells me that ‘line doesn’t have method getElements’. So, two questions:

  1. Should I submit a bugreport in Idea?
  2. Why the error message from Java is like this? Isn’t it able to detect the mistake? What mechanism is working here? Eclipsing of field variable?
  • 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-28T18:23:22+00:00Added an answer on May 28, 2026 at 6:23 pm

    This may be a bug in the Java compiler you are using. According to the Java Language Specification (section 14.14.2), the scope of the variable in an enhanced for statement is the contained statement. (This seems to exclude the iterable or array expression to the right of the :.)

    You can work around the problem by using this.line.getElements().

    EDIT
    Just for kicks, I submitted this as a bug report to Sun. We’ll see whether the Java engineers think it’s a bug in the JLS or in javac (or if they have some other explanation). The bug id is 7139681, although it won’t be posted to the external database for a couple of days.

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

Sidebar

Related Questions

While debugging a piece of code that was exhausting the memory, I found a
I was looking through a code tutorial just now, and found something interesting --
I found an interesting thing. I've setup Nginx for Base Auth with htpasswd file.
While looking on some legacy code, I found a rather unusual construction (for me
I found this interesting thing when I was trying out the new feature optional
Past few days while observing few websites with heavy content I found very interesting
I have found few interesting things (for me at least) while generating random integers
Making a migration from 2.8.1 to 2.9.1 found interesting thing. Tried to write this
Today I found LiquiBase project and I think it is very interesting. I would
Some days ago I looked at boost sources and found interesting typedef . There

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.