Understanding the static Keyword and Singleton Pattern in Java
The static keyword in Java denotes static elements, applicable to member variables and methods. When used, it creates static methods (class methods) and static member variables (class variables). Static member variables are shared across all instances of a class, allowing uniform access and modification.
Static vs. Instance Member Variables
| Aspect | Static Member Variable | Instance Member Variable |
|---|---|---|
| Description | Marked with static, belongs to the class, loads with the class, exists as a single copy in memory, and is shared. |
No static modifier, belongs to each object, with a separate copy per object. |
| Use Case | Suitable for data that needs to be shared and consistent, such as tracking online user counts. | Used for object-speicfic attributes that vary, like name, age, or balance. |
Note: Within the same class, static members can be accessed without specifying the class name.
Static vs. Instance Member Methods
| Aspect | Static Member Method | Instance Member Method |
|---|---|---|
| Description | Marked with static, belongs to the class, loads once with the class, and is shared. |
No static modifier, belongs to objects and requires an object instance for access. |
| Use Case | Ideal for general-purpose functions or operations that don't depend on object state. | Used for behaviors specific to an object, often accessing instance members directly. |
Note: Within the same class, static methods can be called without the class name.
Access Rules for static
Static methods can only access static members and cannot directly reference instance members. Instance methods can access both static and instance members. Additionally, the this keyword is not allowed in static methods.
public class Example {
public static int sharedCount = 5;
private String identifier = "example";
public static void staticMethod() {
System.out.println(Example.sharedCount);
System.out.println(sharedCount);
// System.out.println(identifier); // Error: cannot access instance member directly
Example obj = new Example();
System.out.println(obj.identifier); // Indirect access via object
// System.out.println(this); // Error: 'this' not allowed in static context
}
public void instanceMethod() {
System.out.println(this.identifier);
System.out.println(identifier);
System.out.println(Example.sharedCount);
System.out.println(this.sharedCount); // Not recommended
}
}
Memory Considerations
Static member variables and methods are loaded with the class, prior to object instantiation.
Utility Classes
Utility classes consist entirely of static methods designed for common functionality. They enhance code reusability and development efficiency. It is advisable to make the constructor private to prevent object creation, as methods are accessed via the class name.
Code Blocks
Java classes comprise five components: member variables, methods, constructors, code blocks, and inner classses.
| Type | Static Code Block | Instance Code Block |
|---|---|---|
| Syntax | static { ... } |
{ ... } |
| Characteristics | Belongs to the class, loads with it, executes automatically once. | Belongs to objects, runs before each constructor call during object creation. |
| Purpose | Initializes static resources at program start. | Initializes instance-specific resources. |
Singleton Pattern
The Singleton pattern ensures that a class has only one instance throughout the application, conserving memory. For example, a task menager object requires a single instance to function efficiently.
Eager Singleton
This approach creates the instance upfront, before it is requested.
public class EagerSingleton {
public static EagerSingleton singleton = new EagerSingleton();
private EagerSingleton() {
// Private constructor to restrict instantiation
}
}
Lazy Singleton
This method delays instance creation until the first request.
public class LazySingleton {
private static LazySingleton singleton;
public static LazySingleton getSingleton() {
if (singleton == null) {
singleton = new LazySingleton();
}
return singleton;
}
private LazySingleton() {
// Private constructor to restrict instantiation
}
}