public class example {
int i[] = {0};
public static void main(String args[]) {
int i[] = {1};
change_i(i);
System.out.println(i[0]);
}
public static void change_i(int i[]) {
i[0] = 2;
i[0] *= 2;
}
}
Can anyone tell how the scope of the array int i[] works? I thought if we declare something inside a method it is local to that method.Here int i [] declared inside example(class level) and then inside main method and change_i mehtod.If I run this program it prints 4 . why? Since it is declared inside main method i [] = {1}; I thought it will print 1.Hello Gurus I am bit confused,please help me to understand this?
Thanks
int i[] = {0};
public static void main(String args[]) {
int i[] = {1};
change_i(i);
System.out.println(i[0]);
}
public static void change_i(int i[]) {
i[0] = 2;
i[0] *= 2;
}
}
Can anyone tell how the scope of the array int i[] works? I thought if we declare something inside a method it is local to that method.Here int i [] declared inside example(class level) and then inside main method and change_i mehtod.If I run this program it prints 4 . why? Since it is declared inside main method i [] = {1}; I thought it will print 1.Hello Gurus I am bit confused,please help me to understand this?
Thanks