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
2 odpowiedzi
+ 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.
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)