Complete the code to declare a static variable.
public class Example { static int [1] = 10; }
The variable count is declared as static, meaning it belongs to the class, not instances.
Complete the code to call a static method from the class.
public class Calculator { public static int add(int a, int b) { return a + b; } public static void main(String[] args) { int result = Calculator.[1](5, 3); System.out.println(result); } }
The static method add is called using the class name Calculator.
Fix the error in accessing a non-static variable from a static method.
public class Counter { int count = 0; public static void main(String[] args) { Counter c = new Counter(); System.out.println(c.[1]); } }
To access a non-static variable inside a static method, you need an instance. Here, c.count is correct.
Fill both blanks to create a static method that returns the static variable.
public class Data { static int value = 100; public static [1] getValue() { return [2]; } }
The method returns an int and returns the static variable value.
Fill both blanks to create a class with a static variable, a non-static variable, and a method that prints both.
public class Info { static String [1] = "StaticVar"; String [2] = "InstanceVar"; public void printInfo() { System.out.println([1] + " and " + [2]); } }
The static variable is named staticVar, the instance variable instanceVar, and the method prints both correctly.
