Static methods in Java - Time & Space Complexity
Let's see how the time it takes to run static methods changes as we use them more.
We want to know how the work done grows when calling static methods in a program.
Analyze the time complexity of the following code snippet.
public class Calculator {
public static int square(int n) {
return n * n;
}
public static void printSquares(int[] numbers) {
for (int num : numbers) {
System.out.println(square(num));
}
}
}
This code defines a static method to square a number and another static method to print squares of all numbers in an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array in
printSquaresand callingsquarefor each element. - How many times: Once for each element in the input array.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to square and 10 print operations |
| 100 | 100 calls to square and 100 print operations |
| 1000 | 1000 calls to square and 1000 print operations |
Pattern observation: The work grows directly with the number of elements; doubling the input doubles the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of items you process.
[X] Wrong: "Static methods always run faster and take constant time no matter what."
[OK] Correct: Static means the method belongs to the class, not an object, but the time depends on what the method does and how many times it runs.
Understanding how static methods behave helps you explain code efficiency clearly and confidently in real projects and interviews.
"What if the square method called itself recursively? How would the time complexity change?"
Practice
static methods in Java is true?Solution
Step 1: Understand what static means
Static methods belong to the class itself, not to any object instance.Step 2: Check method calling rules
Static methods can be called using the class name without creating an object.Final Answer:
Static methods belong to the class and can be called without creating an object. -> Option AQuick Check:
Static method = class method call [OK]
- Thinking static methods need an object to call
- Assuming static methods access instance variables
- Believing static methods must return void
Solution
Step 1: Recall Java method declaration order
In Java, the correct order is access modifier, then static keyword, then return type, then method name.Step 2: Match the correct syntax
public static void myMethod() {} follows this order: public static void myMethod() {}.Final Answer:
public static void myMethod() {} -> Option CQuick Check:
Access + static + return type = correct order [OK]
- Placing static after return type
- Omitting access modifier
- Mixing order of keywords
public class Test {
static int count = 0;
static void increment() {
count++;
}
public static void main(String[] args) {
increment();
increment();
System.out.println(count);
}
}Solution
Step 1: Understand static variable and method behavior
The static variablecountstarts at 0 and is shared by all calls.Step 2: Trace method calls
Each call toincrement()increasescountby 1. Called twice, count becomes 2.Final Answer:
2 -> Option AQuick Check:
Two increments = count 2 [OK]
- Thinking count resets each call
- Confusing static and instance variables
- Expecting compilation error due to static usage
public class Example {
int x = 5;
static void printX() {
System.out.println(x);
}
}Solution
Step 1: Check static method access rules
Static methods cannot access instance variables directly because they belong to the class, not an object.Step 2: Identify the error in code
MethodprintX()tries to print instance variablexwithout an object, causing an error.Final Answer:
Cannot use instance variable x inside static method printX. -> Option BQuick Check:
Static method can't access instance variables directly [OK]
- Assuming static methods can access instance variables
- Thinking static methods need return type fix
- Believing classes must be static
MathUtils with a static method square(int n) that returns the square of a number. Which implementation is correct?Solution
Step 1: Identify static method requirements
The method should be static to call without creating an object.Step 2: Check method signature and return type
Method must return int and be declared static with proper access modifier.Step 3: Validate options
public class MathUtils { public static int square(int n) { return n * n; } } correctly declares a public static method returning int.Final Answer:
public class MathUtils { public static int square(int n) { return n * n; } } -> Option DQuick Check:
Static method with return type and public access [OK]
- Omitting static keyword
- Using void return type when returning value
- Declaring class static incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
