21
NovUnderstanding Staic Keyword in Java
Static Keyword in Java
The static keyword in Java is a strong feature that allows you to efficiently manage memory and build class elements that may be shared by all instances. It alters variables, methods, blocks, and even classes so that they belong to the class rather than individual instances of the class. Understanding how to use the static keyword in Java is important for building clean, efficient, and scalable Java code.
Hence, In this Java tutorial, we will explore more about a static keyword in Java including What is a Static Keyword in Java?, Static Variables in Java, and Static Methods in Java. So grab a cup of tea and let's explore each and every concept one by one.
What is a Static Keyword in Java?
In Java, the static keyword indicates that a certain member (variable, function, or block) belongs to the class rather than a single instance. When a member is marked as static, it implies:
In Java, the static keyword can be used for:
Static Variables in Java
A static variable (sometimes called a class variable) is shared by all instances of a class. It is only initialized once, at the beginning of the program execution. All instances of the class can change the value of the static variable, and the changes are reflected across all objects.
Example
public class Counter {
public static int count = 0;
public Counter() {
count++;
}
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
System.out.println("Number of objects created: " + Counter.count);
}
}
Output
Number of objects created: 3
Explanation
The count variable is static and shared by all Counter class objects. Every time a new object is generated, the static count variable is increased. Regardless of how many instances are created, the count variable shows the total number of objects.Static Methods in Java
In Java, static methods belong to the class rather than to its instances. Static methods can only retrieve static data and invoke other static methods; they cannot create a class object. They cannot have direct access to instance variables or non-static methods.Example
public class MathUtils {
// Static method to find the square of a number
public static int square(int number) {
return number * number;
}
public static void main(String[] args) {
// Calling static method without creating an object
int result = MathUtils.square(5);
System.out.println("Square of 5 is: " + result);
}
}
Output
Square of 5 is: 25
Explanation
The square() function is static and can be used directly using the class name (MathUtils.square(5)) without requiring an instance of MathUtils.Static Blocks in Java
To initialize static variables, use a static block (also known as a static initialization block). It is executed when the class is loaded into memory, even before the main function is called. Static blocks are especially useful for establishing initial settings for static variables.Example
public class StaticBlockDemo {
static int num;
static String message;
// Static block
static {
num = 100;
message = "Hello, World!";
System.out.println("Static block executed");
}
public static void main(String[] args) {
System.out.println("Number: " + num);
System.out.println("Message: " + message);
}
}
Output
Static block executed
Number: 100
Message: Hello, World!
Explanation:
Static and Nested Static Classes
In Java, static classes are not permitted; however, you can designate a class as static when it is nested within another class. A nested static class does not require a reference to an instance of the parent class. Static nested classes can only access static members of their parent class.Example
public class OuterClass {
static int outerStaticVar = 10;
// Nested static class
public static class NestedStaticClass {
public void display() {
System.out.println("Static variable from outer class: " + outerStaticVar);
}
}
public static void main(String[] args) {
// Creating an instance of the nested static class
OuterClass.NestedStaticClass nested = new OuterClass.NestedStaticClass();
nested.display();
}
}
Output
Static variable from outer class: 10
Explanation
The NestedStaticClass is a static class, meaning it can be created without an instance of OuterClass. It can directly access static members of the outer class, like outerStaticVar.When to Use the Static Keyword in Java
The static keyword is useful in a variety of circumstances, particularly when you need to share common data or functionality among all instances of a class, or when you wish to access members without establishing an instance.Example
public static final double PI = 3.14159;