File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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"));


}
}
 
GeeCON Prague 2014
 
subject: local variable