Complete the code to declare a static variable named count.
public class Counter { public static [1] count = 0; }
The keyword int declares the variable type. The static keyword is already present.
Complete the code to access the static variable count from the class Counter.
int currentCount = Counter.[1];Static variables are accessed using the class name followed by a dot and the variable name.
Fix the error in the code to correctly increment the static variable count.
public class Counter { public static int count = 0; public void increment() { [1]++; } }
Inside instance methods, static variables should be accessed with the class name to avoid confusion.
Fill both blanks to create a static method that returns the static variable count.
public class Counter { private static int count = 0; public static [1] getCount() { return [2]; } }
The method must return an integer, so its return type is int. The static variable count is returned directly by name.
Fill all three blanks to create a class with a static variable, a constructor that increments it, and a static method to get its value.
public class [1] { private static int count = 0; public [2]() { [3]++; } public static int getCount() { return count; } }
The class name is Counter. The constructor has the same name as the class. The static variable is incremented using Counter.count.
