+ 2

forloop why i can't access int a; why error

public class Program{ public static void main(String[]args){ for(int a = 15; a > 0 ; a-- ){ System.out.println(a); } System.out.println(a); // i can't access int a; here } }

6th Jun 2018, 1:19 PM
youk
3 Answers
+ 4
a can only be accessed inside the loops brackets, the scope where it was declared. Outside the scope it is unknown, just like global and local variables work, too. https://en.m.wikipedia.org/wiki/Scope_(computer_science)
6th Jun 2018, 1:26 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 3
you can not access the local variables outside the its block..
6th Jun 2018, 1:29 PM
Arun Tomar
Arun Tomar - avatar
+ 1
thankx Aaron.....for loop also have scope lilke local or global
6th Jun 2018, 1:33 PM
youk