โInterview Questions
What is the main difference between static and non-static variables in Java?
Static variables belong to the class itself rather than instances. There is only one copy per class that is shared across all instances.
Can static methods access instance variables directly?
No, static methods cannot access instance variables directly. They need a reference to an instance object to access non-static fields and methods.
What happens if a static and instance variable have the same name?
The static variable will be shadowed. To access the static version you must qualify it with the class name.
Can static blocks throw exceptions in Java?
Yes, static blocks can throw exceptions which will propagate outside of the static block's scope once initialized.
Are static nested classes allowed in Java?
Yes, static nested classes are inner classes declared with the static modifier. They do not hold an implicit reference to the enclosing instance.
Can static nested classes access non-static members of the enclosing class?
No, static nested classes cannot access non-static members directly since they do not have a reference to an instance of the enclosing class.
What is the difference between static and non-static inner classes?
Non-static inner classes hold an implicit reference to the enclosing instance. Static nested classes do not.
Can a static method be synchronized in Java?
Yes, static methods can be synchronized to allow thread-safe access to static state shared across threads.
Can static methods be declared final in Java?
Yes, static methods can be declared final to prevent overriding in subclasses since they belong to the class itself.
Can subclasses override a static method defined in a superclass?
No, subclasses cannot override static methods from superclasses. Static methods belong only to the declaring class.
Is a static variable's value shared across all instances of a class?
Yes, the static variable's value is the same across all instances. Changes made by one instance are visible to all others.
Are static variables initialized when a class is loaded or when an instance is created?
Static variables are initialized when the class is first loaded by the JVM, before any instances are created.
Can static blocks access instance methods and variables?
No. Static blocks cannot reference instance methods/variables directly. They can do so through an instance reference.
Where should static variable initialization occur?
Static variable initialization best practice is to do it in a static block rather than field declaration to avoid subtle class loading issues.
Can interfaces define static methods prior to Java 8?
No, static interface methods were added in Java 8. Earlier only static final variables were allowed in interfaces.
Are static imports allowed in Java? If so, why?
Yes, to allow static members to be imported and referenced directly without class qualification.
Can static inner classes access static members of outer classes?
Yes, static inner classes can access static members of their enclosing outer classes.
Can Java enum members be declared as static?
Yes, enum constants and members can be declared static since enums are essentially static classes.
Can a main method be non-static in Java?
No, main must be declared static in order to run without initializing the declaring class first.
Are static members inherited by subclasses in Java?
No, subclasses do not inherit static members from parent classes since they belong only to the declaring class.
Last updated