Correct answer is B.
A static method can only reference other static methods and static fields. Count is non static and therefore produces "error: non-static variable count cannot be referenced from a static context"
I initially thought App.displayMessage() was the issue because an app object hadn't been instantiated. However here app is not an object but rather a class reference to a static method. Static fields do not need an object of that class in order to be used.
This error could also be fixed by making displayMessage non static and creating an app object to reference the method from in main
public class App {
int count;
public static void displayMessage() {
count++;
System.out.println("Welcome. Visit count: " + count);
}
public static void main(String[] args) {
App.displayMessage();
App.displayMessage();
}
}
Correct B is Answer. Because you cannot make a static reference to the non-static field count. To make codes complie need to change "count" into a static variable as "static int count".
upvoted 3 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
MPignaProTech
3 months agoDarGrin
1 year, 3 months agoIbrahimAlnutayfi
1 year, 6 months agoeilla
2 years, 1 month agoiSnover
2 years, 3 months ago