Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Static methods in interfaces
π Scenario: Imagine you are building a simple calculator app. You want to group some utility methods inside an interface to keep your code organized.
π― Goal: You will create an interface with a static method to add two numbers, then call this method from your main program.
π What You'll Learn
Create an interface called Calculator
Add a static method add in Calculator that takes two int parameters and returns their sum
Create a Main class with a main method
Call the static method add from Calculator inside main and print the result
π‘ Why This Matters
π Real World
Static methods in interfaces help organize utility functions related to a concept without needing to create objects.
πΌ Career
Understanding static methods in interfaces is useful for writing clean, modular Java code in professional software development.
Progress0 / 4 steps
1
Create the interface
Create an interface called Calculator with no methods inside.
Java
Hint
An interface is declared with the keyword interface followed by its name.
2
Add a static method
Inside the Calculator interface, add a static method called add that takes two int parameters named a and b and returns their sum.
Java
Hint
Static methods in interfaces have a body and use the keyword static.
3
Create the main class
Create a class called Main with a main method that takes String[] args as parameter.
Java
Hint
The main method is the program entry point and must be declared exactly as public static void main(String[] args).
4
Call the static method and print result
Inside the main method, call the static method add from the Calculator interface with arguments 5 and 7. Store the result in an int variable called sum. Then print sum using System.out.println.
Java
Hint
Use Calculator.add(5, 7) to call the static method and System.out.println(sum) to print the result.
Practice
(1/5)
1. What is the correct way to call a static method calculate() defined inside an interface MathOps?
easy
A. MathOps.calculate()
B. new MathOps().calculate()
C. calculate()
D. MathOps obj = new MathOps(); obj.calculate()
Solution
Step 1: Understand static method call in interfaces
Static methods in interfaces are called using the interface name, not instances.
Step 2: Analyze the options
Only MathOps.calculate() correctly calls the static method. Creating instances or calling directly is invalid.
Final Answer:
MathOps.calculate() -> Option A
Quick Check:
Static method call = InterfaceName.method() [OK]
Hint: Call static interface methods with InterfaceName.method() [OK]
Common Mistakes:
Trying to call static method on an instance
Calling static method without interface name
Trying to instantiate an interface
2. Which of the following is the correct syntax to declare a static method printMessage inside an interface Logger?
easy
A. static void printMessage();
B. static void printMessage() { System.out.println("Hello"); }
C. public static void printMessage();
D. void static printMessage() { System.out.println("Hello"); }
Solution
Step 1: Recall static method syntax in interfaces
Static methods must have a body and use static keyword before return type.
Step 2: Check each option
Only static void printMessage() { System.out.println("Hello"); } correctly declares and defines the static method. Declarations without a body or with static after the return type are invalid.
Final Answer:
static void printMessage() { System.out.println("Hello"); } -> Option B
Quick Check:
Static method = static + return type + name + () + body [OK]
Hint: Static methods in interfaces need a body and static keyword first [OK]
Common Mistakes:
Omitting method body in static method
Placing static keyword after return type
Declaring static methods without body
3. What will be the output of the following code?
interface Helper {
static String greet() {
return "Hi!";
}
}
public class Test {
public static void main(String[] args) {
System.out.println(Helper.greet());
}
}
medium
A. Runtime error
B. Compile-time error
C. null
D. Hi!
Solution
Step 1: Understand static method call in interface
The static method greet() is called correctly using Helper.greet().
Step 2: Predict output
The method returns "Hi!" which is printed by System.out.println.
Final Answer:
Hi! -> Option D
Quick Check:
Static method returns "Hi!" printed [OK]
Hint: Static interface methods run when called by InterfaceName.method() [OK]
Common Mistakes:
Trying to call static method on instance
Expecting compile error due to interface method
Confusing static with default methods
4. Identify the error in the following code snippet:
interface Calculator {
static int add(int a, int b) {
return a + b;
}
}
public class Demo {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 3));
}
}
medium
A. Missing return statement in add()
B. Static method add() cannot be called on instance
C. Cannot instantiate interface Calculator
D. No error, code runs fine
Solution
Step 1: Check interface instantiation
Interfaces cannot be instantiated directly using new.
Step 2: Analyze method call
Static methods must be called using interface name, not instance. But the main error is instantiating interface.
Final Answer:
Cannot instantiate interface Calculator -> Option C
Quick Check:
Interfaces cannot be instantiated [OK]
Hint: Interfaces cannot be created with new keyword [OK]
Common Mistakes:
Trying to instantiate interface
Calling static method on instance
Ignoring compile errors on interface instantiation
5. Given the interface Utils with a static method isEven(int n) that returns true if n is even, how can you use this method inside a class NumberChecker to filter even numbers from a list List<Integer> nums using streams?
hard
A. nums.stream().filter(Utils::isEven).toList();
B. nums.stream().filter(n -> Utils.isEven()).toList();
C. nums.stream().filter(n -> isEven(n)).toList();
D. nums.stream().filter(n -> Utils.isEven(n)).collect();
Solution
Step 1: Understand method reference syntax
Static methods can be referenced as InterfaceName::methodName in streams.
Step 2: Analyze options for correct syntax
nums.stream().filter(Utils::isEven).toList(); uses method reference correctly. The lambda n -> Utils.isEven() misses argument n, n -> isEven(n) lacks interface qualification, and .collect() requires a collector.
Final Answer:
nums.stream().filter(Utils::isEven).toList(); -> Option A
Quick Check:
Use InterfaceName::staticMethod for stream filters [OK]
Hint: Use InterfaceName::methodName for static method references in streams [OK]
Common Mistakes:
Calling static method without argument in lambda
Using instance method syntax for static methods
Wrong terminal operation like collect() without collector