File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "local variable" Watch "local variable" New topic

local variable

Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163

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

Joined: Dec 12, 2005
Posts: 6

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

Rick Allen

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"));

I agree. Here's the link:
subject: local variable
It's not a secret anymore!