Senior
What can be static in Java besides fields?
sobes.tech AI
Answer from AI
In Java, besides static fields, there can be static:
- Methods — belong to the class rather than an instance, called without creating an object.
- Nested classes (static nested classes) — nested classes that are not associated with an instance of the outer class.
- Initializers (static blocks) — code blocks that execute when the class is loaded.
Example:
public class Example {
static int staticField;
static {
// Static initialization block
staticField = 10;
}
static void staticMethod() {
System.out.println("Static method called");
}
static class NestedStaticClass {
void display() {
System.out.println("Inside static nested class");
}
}
}