Static is needed to create members that belong to the class itself, not to any specific object. This helps save memory and allows easy access without creating objects.
Why static is needed in Java
Introduction
Syntax
Java
static dataType variableName; static returnType methodName(parameters) { // method body }
Static members belong to the class, not to any object.
You can access static members using the class name directly.
Examples
Java
static int count = 0;
Java
static void printMessage() { System.out.println("Hello from static method"); }
Java
static final double PI = 3.14159;
Sample Program
This program uses a static variable count to track how many Counter objects are created. The static method showCount prints the current count without needing an object.
Java
public class Counter { static int count = 0; Counter() { count++; } static void showCount() { System.out.println("Count: " + count); } public static void main(String[] args) { Counter c1 = new Counter(); Counter c2 = new Counter(); Counter.showCount(); } }
Important Notes
Static methods cannot access non-static members directly because they belong to the class, not objects.
Use static when you want to share data or behavior across all instances.
Summary
Static members belong to the class, not individual objects.
Static saves memory by sharing data and methods.
Static allows calling methods without creating objects.
Practice
1. Why do we use the
static keyword in Java?easy
Solution
Step 1: Understand what static means
Thestatickeyword means the member belongs to the class itself, not to any individual object.Step 2: Compare with other options
Creating objects, privacy, and inheritance are unrelated tostatic.Final Answer:
To make a member belong to the class rather than any object -> Option BQuick Check:
Static means class-level member = D [OK]
Hint: Static means shared by class, not by objects [OK]
Common Mistakes:
- Thinking static creates objects
- Confusing static with private access
- Assuming static controls inheritance
2. Which of the following is the correct way to declare a static method in Java?
easy
Solution
Step 1: Recall Java method declaration order
In Java, the correct order is access modifier, thenstatic, then return type, then method name.Step 2: Check each option
public static void myMethod() {} follows this order:public static void myMethod(). Others have incorrect order.Final Answer:
public static void myMethod() {} -> Option AQuick Check:
Access modifier + static + return type = A [OK]
Hint: Remember: access modifier before static before return type [OK]
Common Mistakes:
- Placing static after method name
- Omitting static keyword
- Wrong order of keywords
3. What will be the output of this code?
class Test {
static int count = 0;
Test() {
count++;
}
public static void main(String[] args) {
new Test();
new Test();
System.out.println(count);
}
}medium
Solution
Step 1: Understand static variable behavior
The static variablecountis shared by all objects of the class. Each time a new object is created, the constructor incrementscount.Step 2: Count object creations
Two objects are created withnew Test()twice, socountbecomes 2.Final Answer:
2 -> Option DQuick Check:
Static count increments twice = 2 [OK]
Hint: Static variables keep shared count across all objects [OK]
Common Mistakes:
- Thinking count resets for each object
- Confusing static with instance variables
- Expecting compilation error due to static
4. Identify the error in this code:
class Demo {
static void show() {
System.out.println("Hello");
}
public static void main(String[] args) {
show();
Demo d = new Demo();
d.show();
}
}medium
Solution
Step 1: Check static method calls
Static methods can be called using class name or object reference. Bothshow()andd.show()are valid.Step 2: Verify method declaration and usage
The methodshow()has correct return typevoidand usesSystem.out.printlncorrectly.Final Answer:
No error, code runs fine -> Option CQuick Check:
Static methods callable via object or class = B [OK]
Hint: Static methods can be called via object or class name [OK]
Common Mistakes:
- Thinking static methods can't be called on objects
- Assuming missing return type error
- Believing static can't use System.out
5. You want to count how many times a method is called across all objects of a class. Which approach is best?
hard
Solution
Step 1: Understand counting across all objects
To count calls across all objects, the counter must be shared by all instances, so it must be static.Step 2: Evaluate other options
Instance variables count per object, local variables reset each call, and creating new objects won't track method calls properly.Final Answer:
Use a static variable incremented inside the method -> Option AQuick Check:
Shared counter needs static variable = C [OK]
Hint: Use static variable to share count across all objects [OK]
Common Mistakes:
- Using instance variable which counts per object only
- Using local variable which resets every call
- Counting in constructor unrelated to method calls
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
