---
title: "For loop confusion"  
description: "For loop confusion"  
author: "Anonymous User"  
published: 2015-04-28  
updated: 2015-04-28  
canonical: https://www.mindstick.com/forum/23154/for-loop-confusion  
category: "java"  
tags: ["java", "loop"]  
reading_time: 2 minutes  

---

# For loop confusion

I am utterly confused about something I read in the [Oracle](https://www.mindstick.com/articles/13016/alter-table-statement-or-command-in-oracle) OCA book about for loops. One of the "special cases" of [for loop](https://www.mindstick.com/forum/12568/android-for-loop-not-working-in-main-thread) where a [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) is redeclared in the [initialization](https://www.mindstick.com/interview/160/what-is-the-difference-between-assignment-and-initialization) block.

```
package food;/** * * @author Calculus */public class Food {    /**     * @param args the command line arguments     */    public static void main(String[] args) {                 //int x = 0;        //long y = 10;        for(long y = 0, x = 4; x < 5 && y< 10; x++, y++){                     }     }  }
```

\
The code above builds successfully even though I have not given x a data type. To add to my confusion, if I attempt to [remove](https://yourviews.mindstick.com/story/4554/8-harmful-weeds-to-remove-from-garden) the comment from int x = 0; then I incur the [compiler](https://www.mindstick.com/articles/27/just-in-time-compiler)'s wrath. When I remove the comment from long y = 10; same [result](https://www.mindstick.com/blog/12011/advantages-of-getting-result-oriented-seo-from-an-agency), [except](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server) that part makes sense. \
I originally tested this code snippet from my book [thinking](https://www.mindstick.com/blog/11889/how-to-change-your-thinking-paradigm) it might be a typo. Sure enough, it doesn't [compile](https://www.mindstick.com/forum/2316/how-to-views-compile-in-mvc). But it will if I remove long from y in the initialization block and declare it above the loop, then the compiler is happy\

```
int x = 0;        for(long y = 0, x = 4; x < 5 && y< 10; x++, y++){                     }
```

\
Can someone please straighten me out? Why does the top code compile without declaring x?\

## Replies

### Reply by Anonymous User

In for loop init, you can declare multiple variables. In this case, both x and y are of type long. In Java, you can't declare same variable name multiple times as either class field or local variable. This explains why compiler complains when you declare variable x before for loop. \
You can only have one field with a particular name. You can only have one local variable/parameter with a particular name in scope at any one time. But you can have a local variable and a field with the same name and the local variable shadows the field


---

Original Source: https://www.mindstick.com/forum/23154/for-loop-confusion

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
