For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
45. Packages in Java
52. Java Collection
55. Generics In Java
56. Java Interfaces
59. Streams in Java
62. Thread in Java
66. Deadlock in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
77. JUnit Testing
80. Jar file in Java
81. Java Clean Code
85. Java 8 features
86. String in Java
92. HashMap in Java
97. Enum in Java
100. Hashcode in Java
104. Linked List in Java
108. Array Length in Java
110. Split in java
111. Map In Java
114. HashSet in Java
117. DateFormat in Java
120. Java List Size
121. Java APIs
127. Identifiers in Java
129. Set in Java
131. Try Catch in Java
132. Bubble Sort in Java
134. Queue in Java
141. Jagged Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
150. parseInt in Java
152. Abstraction in Java
153. String Input in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
163. Deque in Java
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
176. Square Root in Java
189. Javafx
In Java, the static keyword often confuses beginners—especially when it comes to static methods. Think of a static method like a TV remote: you don’t need a specific TV (object) to understand how the remote (method) works; it's designed to control any compatible TV. Similarly, a static method belongs to the class itself, not to any instance.
In this blog, we’ll explore static methods in Java, how it works, when to use it, and clear up common doubts like “can we override static method in Java?” or “why is the main method static in Java?” Let’s understand!
Enhance your Java skills with expert-led training. Check out upGrad’s Software Engineering course and learn how to apply Java in real-world projects.
In Java programming language, the static keyword means the member (variable, method, block, or nested class) belongs to the class itself, not to any object created from it. This allows access without creating an instance of the class.
Static members are shared among all objects, saving memory and improving performance. Common uses include utility methods (like Math.sqrt()) and constants. The main() method is also static because it runs without creating an object.
Take your Java development career to the cloud. Learn CI/CD, Kubernetes, and more with upGrad’s Cloud Computing and DevOps certificate program.
A static method in Java is a method that belongs to the class rather than any object. It can be invoked directly using the class name and doesn't require an object of the class to be created. It’s commonly used for utility or helper methods.
Example:
public class Utility {
static void greet() {
System.out.println("Hello, Java Developer!");
}
public static void main(String[] args) {
Utility.greet(); // Calling static method without object
}
}
Output:
Hello, Java Developer!
Explanation:
Here, the greet() method is static, so it can be accessed using the class name without creating an object.
Take your Java knowledge to the next level. Explore the Executive Diploma in Data Science & AI by IIIT-B to dive into machine learning, AI, and analytics.
1. Static Block in Java
A static block is a code block that runs once when the class is loaded into memory, even before the main method. It is mainly used to initialize static variables or perform setup operations. Static blocks are executed in the order they appear in the class.
Example:
public class StaticBlockExample {
static {
System.out.println("Static block executed.");
}
public static void main(String[] args) {
System.out.println("Main method executed.");
}
}
Output:
Static block executed.
Main method executed.
Explanation: The static block runs automatically when the class is loaded, even before the main() method is executed. It’s a great place for initializing static data or logging when a class is first accessed.
2. Static Variable in Java
A static variable is shared across all instances of a class. It belongs to the class itself rather than any object, so all objects refer to the same memory location. It's often used for common values like counters, constants, or configuration values that should be shared.
Example Code:
public class StaticVariableExample {
static int count = 0;
StaticVariableExample() {
count++;
System.out.println("Object count: " + count);
}
public static void main(String[] args) {
new StaticVariableExample();
new StaticVariableExample();
}
}
Output:
Object count: 1
Object count: 2
Explanation:Here, the count variable is static, so its value is shared among all objects. Each time a new object is created, the same count is incremented and printed, reflecting the total number of objects created.
A static method in Java belongs to the class and not any object. It can be called using the class name and can only access static variables or call other static methods directly. It’s useful for utility functions or operations that don’t depend on instance-specific data.
Example Code:
public class StaticMethodExample {
static void greet() {
System.out.println("Hello from static method!");
}
public static void main(String[] args) {
StaticMethodExample.greet();
}
}
Output:
Hello from static method!
Explanation:The greet() method is static, so it can be called without creating an object. It's invoked using the class name, showing that the method belongs to the class, not any instance.
Use static variables when a value should be shared among all class instances, like counters or constants. Use static methods when the behavior doesn't rely on instance variables. This improves memory efficiency and allows direct access via the class name, making them ideal for utility or helper functions.
Example:
public class Utility {
static int square(int n) {
return n * n;
}
public static void main(String[] args) {
System.out.println("Square of 5: " + Utility.square(5));
}
}
Output:
Square of 5: 25
Explanation:
The static method square() performs a calculation without relying on any object data. It's accessed directly through the class, making it a good fit for stateless utility operations.
Explanation: In Java, only nested classes can be declared static. A static nested class can access static members of the outer class but not non-static ones. Static nested classes are often used for grouping helper classes logically without needing an object of the outer class.
Example:
public class Outer {
static int data = 30;
static class Inner {
void show() {
System.out.println("Data: " + data);
}
}
public static void main(String[] args) {
Outer.Inner obj = new Outer.Inner();
obj.show();
}
}
Output:
Data: 30
Explanation:The inner class is static, so it can be accessed without creating an object of the outer class. It accesses the static variable data from the outer class, which shows the purpose of a static nested class.
No, static methods cannot be overridden because they are bound to the class, not to objects. If you define a static method in the subclass with the same name, it hides the parent method (method hiding), not override.
Example:
class Parent {
static void show() {
System.out.println("Parent static method");
}
public static void main(String[] args) {
Parent.show(); // Output: Parent static method
Child.show(); // Output: Child static method
}
}
class Child extends Parent {
static void show() {
System.out.println("Child static method");
}
}
Output:
Parent static method
Child static method
Explanation:
Static methods are not overridden; they are hidden when redefined in a subclass. In this code, calling show() on each class separately executes the version defined in that class, showcasing that static methods are class-bound, not object-bound.
Also read: Overloading vs Overriding in Java
The main method is static because it allows the JVM to call it without creating an instance of the class. Since execution starts from main(), it must be accessible even before any objects are created.
Example:
public class MainExample {
public static void main(String[] args) {
System.out.println("Program started without creating an object.");
}
}
Output:
Program started without creating an object.
Explanation:
The main method runs first when a Java program starts. Declaring it static allows the JVM to invoke it directly using the class name—without needing an object—making it the official program entry point.
Understanding how and when to use the static method in Java is crucial for writing efficient and memory-optimized code. The static keyword is foundational in Java programming, from the main method to utility classes. Whether you're wondering "can we override the static method in Java" or "why is the main method static in Java," we hope this guide clarifies everything.
A static method belongs to the class rather than any instance of the class. It can be invoked using the class name, without needing an object. Static methods are typically used for utility or helper functions that do not depend on instance variables.
No, static methods cannot be overridden. They are resolved at compile time, and method calls are bound to the class type, not the instance type. While method hiding can occur in inheritance, it isn't true overriding.
The main method is static because it allows the JVM to invoke it without creating an instance of the class. This is crucial for the program’s execution since the main method serves as the entry point to the application.
No, static methods cannot directly access instance variables or instance methods. They can only access static variables and methods. To access instance variables, a static method would need to use an object of the class.
What is method overloading with static methods in Java?
Static methods can be overloaded, meaning multiple static methods with the same name but different parameter lists can exist in a class. Overloading is determined by the method signature, allowing different behaviors based on input.
Yes, static methods can be called from instance methods, though it is recommended to use the class name for clarity. Static methods are accessible to instance methods as they belong to the class and not an instance.
Static methods are commonly used for utility functions that do not require object states. They can also be used for tasks like managing shared resources or providing global access points to methods, such as in singleton patterns.
No, a static method cannot use the this keyword because it does not belong to any specific instance of the class. The this keyword refers to the current object, but static methods do not have access to object-level context.
Static methods are inherited but are not overridden. If a static method in a subclass has the same signature as a static method in the parent class, it hides the parent method. This is known as method hiding, not overriding.
No, constructors cannot be static in Java. Constructors are used to create instances of a class, and a static method cannot operate on a specific instance. Declaring a constructor as static would contradict its purpose.
Static methods are stored in the method area of the JVM memory, which is shared by all instances of the class. Since they belong to the class rather than individual objects, static methods are loaded into memory once when the class is loaded.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.