This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring 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