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; } }

26th Aug 2020, 5:17 PM
anshu kulshrestha
anshu kulshrestha - avatar
2 ответов
+ 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); } }
26th Aug 2020, 7:41 PM
zemiak
+ 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
26th Aug 2020, 5:51 PM
Jayakrishna 🇮🇳