This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Usage of "final" in enhanced for loop.

 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Here is my programme which is compiling and working fine.

<code>
public class EnhancedForTest {
public static void main(String... args) {
String[] strArr = {"A", "B", "C", "D"};

for (final String s : strArr) {
System.out.println(s);
}
}
}
</code>

Since the String s is delcared as final, this code should not compile. However, this is working fine. Is it creating a new string object for every iteration because assignment is not possible second time for final variables?

Thanks & Regards,
- Surya.
 
Santhosh Raje
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sai,
The Final Reference Variable which is pointed to String array object is not changed.So no error occurs.Since I dont know how the for each method is internally implemented I cant say anything beyond this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!