What is the static
Keyword?
In Java, the static
keyword is used to indicate that a particular member (variable or method) belongs to the class itself, rather than to instances of the class. This means that the static
member can be accessed without creating an instance of the class.
Static Variables
A static variable is shared among all instances of a class. It is also known as a class variable.
Example:
public class Example {
// static variable
static int count = 0;
Example() {
count++;
}
public static void main(String[] args) {
Example obj1 = new Example();
Example obj2 = new Example();
Example obj3 = new Example();
System.out.println("Count: " + Example.count); // Output: Count: 3
}
}
In this example, the count
variable is static and shared among all instances of the Example
class. It keeps track of the number of instances created.
Static Methods
A static method belongs to the class rather than any particular object instance. You can call a static method without creating an object of the class.
Example:
public class Example {
// static method
static void displayMessage() {
System.out.println("Hello, World!");
}
public static void main(String[] args) {
Example.displayMessage(); // Output: Hello, World!
}
}
In this example, the displayMessage
method is static, so it can be called directly using the class name without creating an instance of the Example
class.
Static Block
A static block is used for static initialization of a class. This block is executed when the class is first loaded into the memory.
Example:
public class Example {
static int num;
static String str;
// static block
static {
num = 100;
str = "Static Block Initialization";
}
public static void main(String[] args) {
System.out.println("Number: " + Example.num); // Output: Number: 100
System.out.println("String: " + Example.str); // Output: String: Static Block Initialization
}
}
In this example, the static block initializes the static variables num
and str
when the class is loaded.
Static Nested Class
A static nested class is a static class defined within another class. It can access the static members of the outer class.
public class OuterClass {
static int data = 30;
static class InnerClass {
void display() {
System.out.println("Data: " + data);
}
}
public static void main(String[] args) {
OuterClass.InnerClass inner = new OuterClass.InnerClass();
inner.display(); // Output: Data: 30
}
}
In this example, InnerClass
is a static nested class within OuterClass
, and it can access the static variable data
of the outer class.
Key Points to Remember
- Static variables and methods belong to the class and can be accessed without creating an instance of the class.
- Static methods can only directly access other static members (variables and methods).
- Static blocks are executed when the class is loaded and are used for static initialization.
- Static nested classes can access the static members of the outer class.
- The
static
keyword cannot be used with local variables inside methods
Comments
Post a Comment