0
Shouldn't it be a compilation error.Here i am defining "a "again rather then only initializing it
public class Program { int a=4; public static void main(String[] args) { new Program().go(); } void go(){ int a=4; } }
2 Respuestas
+ 2
the first a is class variable a
second is local variable with same name
public class Program {
int a=4;
public static void main(String[] args) {
var p = new Program();
p.go();
System.out.println(p.a +" main");
}
void go(){
int a=20;
System.out.println(a);
System.out.println(this.a);
}
}
+ 1
You declared it in method level, that hides class level variable a.
In function, if you write
System.out.println (a); will print method variable 'a' value.
By With class object calling, it will print class level variable 'a' value