What is the use of the static keyword in Java

๐Ÿ’ก Concept: Static Keyword in Java

The static keyword is used to create class-level variables and methods that belong to the class rather than instances.

๐Ÿ“˜ Quick Intro

Static members are shared across all instances of a class and can be accessed without creating an object.

๐Ÿง  Analogy

Think of static members as shared tools in a workshop: everyone uses the same hammer instead of having individual ones.

๐Ÿ”ง Technical Explanation

  • Static variables: Shared among all instances.
  • Static methods: Can be called without an object.
  • Static blocks: Used for static initialization.
  • Static nested classes: Nested classes declared static.
  • Useful for utility or helper methods and constants.

๐ŸŽฏ Use Cases

  • โœ… Define constants with static final.
  • โœ… Utility methods like Math.sqrt() are static.
  • โœ… Maintain counters shared across instances.

๐Ÿ’ป Java Static Keyword Example


class Counter {
    static int count = 0;

    Counter() {
        count++;
    }

    static void displayCount() {
        System.out.println("Count: " + count);
    }
}

public class Main {
    public static void main(String[] args) {
        new Counter();
        new Counter();
        Counter.displayCount(); // Count: 2
    }
}

โ“ Interview Q&A

Q1: What does static mean in Java?
A: Belongs to class, not instance.

Q2: Can static methods access instance variables?
A: No.

Q3: When are static blocks executed?
A: When class loads.

Q4: Can static methods be overridden?
A: No, they are hidden.

Q5: What is a static nested class?
A: Nested class declared static.

Q6: Are static variables thread-safe?
A: No, need synchronization.

Q7: Can you access static members without object?
A: Yes.

Q8: Can static variables be final?
A: Yes, constants.

Q9: Difference between static and instance variables?
A: Static shared; instance per object.

Q10: Use of static keyword?
A: Class-level shared members.

๐Ÿ“ MCQs

Q1. What does static mean in Java?

  • Belongs to instance
  • Belongs to class, not instance
  • Both
  • None

Q2. Can static methods access instance variables?

  • Yes
  • No
  • Sometimes
  • Depends

Q3. When are static blocks executed?

  • At runtime
  • When class loads
  • When object created
  • Never

Q4. Can static methods be overridden?

  • Yes
  • No
  • Sometimes
  • Depends

Q5. What is a static nested class?

  • Inner class
  • Nested class declared static
  • Anonymous class
  • None

Q6. Are static variables thread-safe?

  • Yes
  • No
  • Sometimes
  • Always

Q7. Can you access static members without object?

  • No
  • Yes
  • Sometimes
  • Depends

Q8. Can static variables be final?

  • No
  • Yes
  • Sometimes
  • Depends

Q9. Difference between static and instance variables?

  • Same
  • Static shared; instance per object
  • Instance shared
  • None

Q10. Use of static keyword?

  • Instance members
  • Class-level shared members
  • Local variables
  • None

๐Ÿ’ก Bonus Insight

Static keyword enables shared resources and utility methods without creating objects, important for memory efficiency and design.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

Learn More About Java โ˜•

What is Java and its key features ๐Ÿ‘‰ Explained
Explain the Java Virtual Machine (JVM) ๐Ÿ‘‰ Explained
Difference between JDK, JRE, and JVM ๐Ÿ‘‰ Explained
What are Javaโ€™s main data types ๐Ÿ‘‰ Explained
Explain the concept of Object-Oriented Programming in Java ๐Ÿ‘‰ Explained
What is the difference between a class and an object ๐Ÿ‘‰ Explained
Explain encapsulation with an example ๐Ÿ‘‰ Explained
What is inheritance in Java and its types ๐Ÿ‘‰ Explained
Define polymorphism in Java with examples ๐Ÿ‘‰ Explained
What is abstraction in Java ๐Ÿ‘‰ Explained
Difference between abstract class and interface in Java ๐Ÿ‘‰ Explained
Explain method overloading and method overriding in Java ๐Ÿ‘‰ Explained
What are constructors in Java ๐Ÿ‘‰ Explained
What is the use of the static keyword in Java ๐Ÿ‘‰ Explained
Explain the difference between final, finally, and finalize in Java ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup