This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes local variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "local variable" Watch "local variable" New topic
Author

local variable

Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163


Hi
variables defined in a for-loop are local to the forloop. Code outside the for loop can't access the local variables defined in a for-loop.

But the above program is giving compilation error. Can any one of u tell me the reason?
Cristina Vinuela
Greenhorn

Joined: Dec 12, 2005
Posts: 6
Hi!

But the loop can access the method variables, that's why the compiler finds two i variables IN the loop, the local one and the method one.

Hope it helps

Cris
Rick Allen
Greenhorn

Joined: Dec 27, 2005
Posts: 1
I arranged the code given and hand no compiler error. Hope this helps.

class Test{

int i=5;


public static void main(String args[]) {

for(int i=1;i<=5;i++,System.out.println("welcome"));


}
}
 
 
subject: local variable
 
Similar Threads
Local variable scope
doubt Method local inner class
Scope of variable declarartions in for loop
Regarding contiue keyword
Program