0

What is the output of following program Options: a) 5 b) garbage c) 0 d) none of the above

Please explain me. I am not able to understand the output. Here is the code. https://code.sololearn.com/ce2w64EftOBQ/?ref=app

29th Oct 2020, 6:43 AM
Atul Gautam
Atul Gautam - avatar
2 Answers
+ 1
You just need to declare the variable static and use the class name to access it. class A{ static int a =5; } class Test{ public static void main(String[] args) { System.out.println(A.a); } } By the way your code will not even compile.
29th Oct 2020, 7:21 AM
Avinesh
Avinesh - avatar
0
there are two different classes, The A has declared the a (and it is non-static) create the object of A for access its (non-static) fields A objA = new A(); System.out.println(objA.a); (this is another way how to solve it)
29th Oct 2020, 1:43 PM
zemiak