Java Static Class Members
Consider the following code.
public class DataProcessor {
private static int count = 0;
public static void incrementCount() {
count++;
}
public static int getCount() {
return count;
}
public static void resetCount() {
count = 0;
}
}
public class Main {
public static void main(String[] args) {
DataProcessor.incrementCount();
DataProcessor.incrementCount();
int first = DataProcessor.getCount();
DataProcessor.resetCount();
DataProcessor.incrementCount();
int second = DataProcessor.getCount();
System.out.println(first + " " + second);
}
}
Which statement best explains why the static methods in the DataProcessor class can access and modify the static count variable?
A
The code works because each method call creates a new instance of DataProcessor automatically
B
The methods work because they are called from another static method (main), creating a static context
C
Static methods can access static variables because both belong to the class rather than to any specific instance
D
Static methods can only read static variables but cannot modify them without creating an object
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | kaisuki | 1 | 1 | 0m 00s | 100 |
| #2 | chunxiangxu.cxu | 1 | 1 | 0m 12s | 88 |
| #3 | winstonhou1107 | 1 | 1 | 0m 55s | 45 |
| #4 | zgj07310417 | 1 | 1 | 1m 03s | 37 |
| #5 | lsj08030922 | 1 | 1 | 1m 25s | 15 |
| #6 | richa.tuli | 0 | 1 | 0m 44s | -54 |
| #7 | bommasam000 | 1 | 4 | 14m 29s | -799 |
Items per page:
10
1 – 7 of 7
APFIVE