0% found this document useful (0 votes)
6 views

Static Keyword

Static keyword in Java.

Uploaded by

pandeyaman684
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Static Keyword

Static keyword in Java.

Uploaded by

pandeyaman684
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1. What is the static keyword in Java?

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.

2. What is a static variable in Java?

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.

3. What is a static method in Java?

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.

4. Can static methods access instance variables or 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.

5. Can you override a static method?

A: No, static methods cannot be overridden because method overriding is based on dynamic (runtime)
binding, whereas static methods are bound at compile time.

6. Can a static method be inherited?

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.

7. What happens if a static variable is modified?

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.

8. What is the difference between a static variable and an instance variable?

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.

9. Can we declare a constructor as static in Java?

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.

10. Can we create static classes in Java?


In Java, you cannot declare top-level classes as static. However, nested (inner) classes can be static. A static nested
class does not have access to the instance variables and methods of the outer class unless it is explicitly passed an
instance of the outer class.

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

You might also like