Given:
What is the result?
A. Compilation fails at line n3 and line n4.
B. Compilation fails at line n1 and line n2,
C. Welcome Visit Count:1 Welcome Visit Count: 2
D. Welcome Visit Count:1 Welcome Visit Count: 2
Given:
What is the result?
A. Compilation fails at line n3 and line n4.
B. Compilation fails at line n1 and line n2,
C. Welcome Visit Count:1 Welcome Visit Count: 2
D. Welcome Visit Count:1 Welcome Visit Count: 2
B :
# javac Test36.java
Test36.java:4: error: non-static variable count cannot be referenced from a static context
count++;
^
Test36.java:5: error: non-static variable count cannot be referenced from a static context
System.out.println(“Welcome ” + “Visit Count: “+ count);
^
2 errors
———————————-
per suggestion from the compiler, it is needed to declare as “static int count”, then it will be possible to compile the program and get such result:
#javac Test36.java
#java Test36
Welcome Visit Count: 1
Welcome Visit Count: 2
Answer B:
We cannot call non-static members from static context.
If we declare a variable outside a static method and use it inside the method, we must make it static and we are allowed to not initialize it. However, if we declare a variable inside a static method we cannot make it static and we have to initialize it. So the answer is B
B