Static Methods In Class
Static Methods In Class
A school program needs reliable data and clear rules. A static method does not require an instance receiver. It suits a marks calculation that needs only explicit inputs.
Understand
A static method does not require an instance receiver.
Apply
Trace the Java example, predict its output and explain the result before running it.
Avoid mistakes
A static method cannot use an instance field without an object reference. Static is not a guarantee of purity or safety.
Static Methods In Class
Math.sqrt() or Integer.parseInt(), you've already used static methods.
Static methods are useful when the behavior doesn’t depend on instance data (i.e., member variables). They're ideal for utility or helper operations.
How to Define a Static Method
static keyword before the return type:
public class MathUtils {
public static int square(int x) {
return x * x;
}
}
You can call this method directly using the class name:
int result = MathUtils.square(5);
System.out.println(result);
25
Static Method vs Instance Method
public class Hello {
public static void sayStaticHello() {
System.out.println("Hello from static!");
}
public void sayInstanceHello() {
System.out.println("Hello from instance!");
}
public static void main(String[] args) {
// Calling static method
Hello.sayStaticHello();
// Calling instance method
Hello h = new Hello();
h.sayInstanceHello();
}
}
Hello from static!
Hello from instance!
When to Use Static Methods
- The method logic does not rely on object state.
- You are writing utility/helper methods.
- You need global access to common behavior.
Accessing Static Methods from Another Class
public class Greet {
public static void sayHi() {
System.out.println("Hi there!");
}
}
public class Main {
public static void main(String[] args) {
Greet.sayHi();
}
}
Hi there!
Calling Static Methods from Instance Methods
public class Demo {
public static void showStatic() {
System.out.println("Static method called");
}
public void showInstance() {
System.out.println("Calling from instance method...");
showStatic(); // or Demo.showStatic();
}
public static void main(String[] args) {
new Demo().showInstance();
}
}
Calling from instance method...
Static method called
Limitations of Static Methods
- They cannot access non-static (instance) variables directly.
- They cannot use
thisorsuper. - They are not polymorphic — cannot be overridden like instance methods.
public class Test {
int x = 100;
public static void display() {
// System.out.println(x); // Compile-time error
System.out.println("Static method can't access instance variables directly");
}
}
Static Block vs Static Method
- A static block is executed once when the class is loaded.
- A static method can be called multiple times explicitly.
public class Example {
static {
System.out.println("Static block executed");
}
public static void greet() {
System.out.println("Static method executed");
}
public static void main(String[] args) {
Example.greet();
Example.greet();
}
}
Static block executed
Static method executed
Static method executed
Best Practices for Static Methods
- Keep them stateless — don’t rely on global data.
- Use them for shared utilities or mathematical operations.
- Avoid overusing static methods in OOP-heavy designs.
Common Examples in Java API
Math.abs(),Math.max()Collections.sort()Integer.parseInt()Thread.sleep()
These are all static — no need to instantiate the class.
Conclusion
Important boundaries and mistakes
A static method cannot use an instance field without an object reference. Static is not a guarantee of purity or safety.
Try the school example
Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: A static method cannot use an instance field without an object reference.
Java 17 · Main.java · complete practice program
public class Main {
static boolean pass(int mark) {
return mark >= 50;
}
public static void main(String[] args) {
System.out.println(pass(72));
}
}
Worked answer and reasoning
Java 17 · Main.java · source repeated for this answer
public class Main {
static boolean pass(int mark) {
return mark >= 50;
}
public static void main(String[] args) {
System.out.println(pass(72));
}
}
Expected output
trueA static method does not require an instance receiver. It suits a marks calculation that needs only explicit inputs.
A static method cannot use an instance field without an object reference. Static is not a guarantee of purity or safety.
Run with javac Main.java, then java Main. The declaration of Main names the class; main starts the example, and println writes a complete output line.
Check your understanding
What to remember
A static method does not require an instance receiver. It suits a marks calculation that needs only explicit inputs. A static method cannot use an instance field without an object reference. Static is not a guarantee of purity or safety.
Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide and Java 17 language specification.
Reaching the recap and completing the quizzes are separate milestones.