0
0
Javaprogramming~15 mins

Static variables in Java - Interactive Code Practice

Choose your learning style8 modes available
ads_clickPractice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a static variable named count.

Java
public class Counter {
    public static [1] count = 0;
}
🎯 Drag options to blanks, or click blank then click option
Afinal
Bstatic
Cvoid
Dint
Attempts:
3 left
2fill in blank
medium

Complete the code to access the static variable count from the class Counter.

Java
int currentCount = Counter.[1];
🎯 Drag options to blanks, or click blank then click option
Acount
BgetCount()
Cthis.count
DCounter()
Attempts:
3 left
3fill in blank
hard

Fix the error in the code to correctly increment the static variable count.

Java
public class Counter {
    public static int count = 0;
    public void increment() {
        [1]++;
    }
}
🎯 Drag options to blanks, or click blank then click option
ACounter.count
Bcount
Cthis.count
Dincrement
Attempts:
3 left
4fill in blank
hard

Fill both blanks to create a static method that returns the static variable count.

Java
public class Counter {
    private static int count = 0;
    public static [1] getCount() {
        return [2];
    }
}
🎯 Drag options to blanks, or click blank then click option
Aint
Bcount
Cthis.count
Dvoid
Attempts:
3 left
5fill in blank
hard

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.

Java
public class [1] {
    private static int count = 0;
    public [2]() {
        [3]++;
    }
    public static int getCount() {
        return count;
    }
}
🎯 Drag options to blanks, or click blank then click option
ACounter
CCounter.count
Dcount
Attempts:
3 left