static and non-static members - Notes by Shariq SP

Static and Non-Static Members in Java Classes

In Java, members within a class can be classified as static or non-static. Understanding the difference between static and non-static members is essential for proper design and implementation of Java classes. Let's explore the characteristics, usage, and implications of static and non-static members.

Static Members

Static members belong to the class rather than individual instances of the class. They are shared among all instances of the class and can be accessed using the class name. Static members are initialized only once, at the start of the program's execution, and retain their value until the program terminates.

Usage:

  • Static variables (also known as class variables) are used for values that are common to all instances of the class, such as constants or shared data.
  • Static methods (also known as class methods) are used for utility functions that do not require access to instance-specific data.
  • Static blocks are used for initializing static variables or performing one-time initialization tasks for the class.

Non-Static Members

Non-static members are associated with individual instances of the class. Each instance of the class has its own copy of non-static members, which are independent of other instances. Non-static members are accessed using object references and can have different values for each instance.

Usage:

  • Instance variables (also known as member variables) are used to represent state or data specific to each instance of the class.
  • Instance methods are used to perform operations that require access to instance-specific data.

Implications

  • Static members are shared among all instances of the class, while non-static members are unique to each instance.
  • Static members can be accessed using the class name, while non-static members require an instance of the class.
  • Changes to static members affect all instances of the class, while changes to non-static members affect only the specific instance.

Interview-Based Questions

  1. What is the difference between static and non-static members in Java?
  2. When should you use static variables?
  3. Explain the concept of static methods with an example.
  4. Why are static blocks used in Java?
  5. What is the significance of non-static members in a Java class?
  6. Discuss scenarios where you would prefer non-static methods over static methods.
  7. Can a non-static method access a static member of the class?
  8. Explain how static and non-static members affect memory usage in Java.

Multiple Choice Questions (MCQs)

  1. Which type of members belong to the class rather than individual instances?
    1. Static
    2. Non-static
    3. Both
    4. None
  2. When are static members initialized in Java?
    1. At the start of the program's execution
    2. At the end of the program's execution
    3. When the object is created
    4. When the object is destroyed
  3. How are static members accessed in Java?
    1. Using object references
    2. Using class name
    3. Both
    4. None
  4. Which type of member is unique to each instance of the class?
    1. Static
    2. Non-static
    3. Both
    4. None
  5. Which type of member is shared among all instances of the class?
    1. Static
    2. Non-static
    3. Both
    4. None