What is Static Keyword in Java? Various Applications Explained
By Sriram
Updated on Jul 03, 2023 | 9 min read | 6.7k views
Share:
For working professionals
For fresh graduates
More
By Sriram
Updated on Jul 03, 2023 | 9 min read | 6.7k views
Share:
Table of Contents
Static is a reserved keyword in Java. This blog will explain how we can apply a static keyword in Java to various programming aspects, including variables, blocks, methods, and nested classes.
In Java, when a static member indicates that it belongs to a type itself instead of being an instance of that type. It means that only one instance of a static member is created and shared across all class instances.
The primary method of the class is generally static. The static keyword in Java is used for a constant variable or a method that is the same for all class instances. The keyword is primarily used for memory management. Learn more about Java architecture and components.
One of the primary use cases of static variables is maintaining shared data across class instances. For instance, if we have a class representing a bank, a static variable can keep track of the total number of bank accounts created, as it is information shared among all account objects.
Static variables are commonly used to define constants in Java. By declaring a variable as static final, we ensure that its value remains constant throughout the execution of the program. These constants can be accessed without creating an instance of the class, making them easily accessible in various parts of the program.
The static keyword is also used to define utility methods that perform specific tasks and are not associated with any particular object. Utility methods often provide common functionality and can be called using the class name. Examples include mathematical calculations, string manipulation, and file operations.
The static keyword is employed in implementing the Singleton design pattern, where only one instance of a class should exist in the entire program. By defining the constructor as private and providing a static method to access the single instance, we ensure that no other instances can be created. The static instance variable stores the sole instance of the class.
The use of static methods and variables can lead to performance improvements. Since static members belong to the class, they are loaded and initialized once, reducing memory consumption and avoiding redundant memory allocation for each object.
In Java, the static keyword plays a crucial role in defining class-level members and their behaviour. Static variables enable the sharing of data among all instances of a class, while static methods provide utility and common functionality accessible without object instantiation. Understanding the purpose and applications of the static keyword is essential for writing efficient and well-structured Java programs. By leveraging static members effectively, developers can design scalable applications with optimized memory usage and improved performance.
A few examples pf the usage of static keywords in Java includes:
Static Variable:
public class Counter {
private static int count;
public void increment() {
count++;
}
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
c1.increment();
System.out.println("Count: " + c1.count); // Output: 1
c2.increment();
System.out.println("Count: " + c2.count); // Output: 2
}
}
Static Method:
public class MathUtils {
public static int sum(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = MathUtils.sum(5, 10);
System.out.println("Sum: " + result); // Output: 15
}
}
Static Constant:
public class Constants {
public static final double PI = 3.14159;
public static void main(String[] args) {
System.out.println("The value of PI is: " + Constants.PI); // Output: The value of PI is: 3.14159
}
}
Singleton Pattern:
public class Singleton {
private static Singleton instance;
private Singleton() {
// Private constructor to prevent direct instantiation
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
Similarly, if you’re wondering what is static variable in Java, it is a variable that belongs to the class, not the instance of the class. This means that only one copy of the static variable exists, no matter how many instances of the class exist in your program. Static variables are typically used for constants or for maintaining state information within a class across all its instances. They can also be used to provide access control by making them private and then providing public static methods to manipulate them. Furthermore, static variables can be used to optimize memory usage since multiple objects will share the same variable rather than having each object store its own copy.
Static members can be created by preceding their declarations with the keyword static. Any member declared as static can be accessed before any objects of its class are created without referencing any object.
The static keyword in Java is a non-access modifier. It can be used for –
Static variables are primarily global variables. When you define a variable as static, a single copy of the variable is created and shared among all objects at the class level. Static variables are created at the class level, and all the instances of the class share the same static variable.
Read: Pattern Programs in Java
When a method is declared with the keyword static, it’s known as a static method. Static methods have the following restrictions –
If you want to share specific code across all class instances, you need to write the code in a static method.
Static methods are also used to create helper or utility classes so that one doesn’t need to create a new object of these classes for obtaining them.
As static methods don’t need an object for invoking Java, the main method is static. Had it not been static, JVM would need to have created an object before calling the main(), leading to extra memory allocation.
Static blocks make it easy to initialize variables with predefined values. The following code demonstrates the usage of a static block that contains a variable declaration that is executed exactly once when the class is loaded.
Must Read: Java Project Ideas & Topics
Only a nested class can be made static. A nested static class doesn’t need a reference for the outer class.
Enroll in Software Engineering Courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
This blog explained the static keyword in Java and its use in variables, methods, blocks, and nested classes.
If you’re interested to learn more about Java, full-stack software development, check out upGrad & IIIT-B’s Executive PG Program in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources