Static Keyword
Static Keyword
Answer: The static keyword in Java is used to indicate that a method, variable, or block belongs to the class
rather than instances (objects) of the class.
This means static members can be accessed without creating an instance of the class.
Answer: A static variable is a class-level variable that is shared among all instances of a class. Unlike instance
variables, a static variable is created when the class is loaded into memory and it remains in memory for the
entire life of the application. All instances of the class share the same copy of the static variable.
Answer: A static method belongs to the class rather than any specific instance. It can be called without creating
an instance of the class. Static methods can access static variables and other static methods but cannot directly
access instance variables or instance methods.
A: No, static methods can only access static variables and other static methods because static methods do not
belong to any instance of the class.
A: No, static methods cannot be overridden because method overriding is based on dynamic (runtime)
binding, whereas static methods are bound at compile time.
A: Yes, static methods can be inherited, but they cannot be overridden. If a subclass defines a static method
with the same signature, it will hide the super class’s static method.
A: A static variable is shared across all instances of the class. If it is modified, the change is reflected across all
instances of the class.
o Static variable: Shared by all instances of the class and exists at the class level.
o Instance variable: Specific to each instance of the class and exists at the object level.
No, constructors cannot be static because they are used to initialize objects, and static members belong to the
class, not instances. Constructors are invoked when creating instances, so it wouldn't make sense for them to be
static.
Key Points:
Static variables and methods are shared across all instances of a class.
Static methods cannot access non-static (instance) variables and methods directly.
Static blocks are used to initialize static data and are executed when the class is loaded.
Static nested classes can be used without creating an instance of the outer class.
Access Modifier,
Oops
String
Super keyword
Protected