Javatpoint Logo
Javatpoint Logo

Difference Between Static and Non-Static Members in Java

Java, a widely used object-oriented programming language, offers a variety of features to help build robust and flexible applications. Two important concepts in Java of the object model have static and non-static members. Understanding the difference between static and non-static members is important for effective Java programming. In this section, we will explore these concepts in more detail and explore their distinctive characteristics.

Static Members:

In Java, static members belong to a class rather than instances of a class. These members are distributed across all instances of the class and are initialized only once, when the class is loaded. There are three main types of static members in Java: static variables, static methods, and static blocks.

Static Variables:

Static variables, also known as class variables, are declared using the static keyword.

They are shared among all instances of the class, allowing changes made by one instance to be visible to all other instances.

Static variables are initialized only once when the class is loaded.

Static Methods:

Static methods are associated with the class rather than with an instance of the class.

They can be invoked using the class name without creating an instance of the class.

Common use cases for static methods include utility methods and factory methods.

Static Blocks:

Static blocks are used to initialize static variables or perform one-time actions when the class is loaded.

They are executed only once, regardless of the number of instances created.

Non-Static Members:

Static members, on the other hand, are associated with instances of the class. Each instance has its own copy of volatile members, and they are initialized when each instance is created. There are two main types of volatile members: instance variables and instance methods.

Instance Variables:

Instance variables are declared without the static keyword.

Each instance of the class has its own copy of instance variables, and changes made to these variables are unique to each instance.

Instance Methods:

Instance methods are associated with an instance of the class and have access to instance variables.

They are invoked on instances of the class and can interact with the state of the object.

Key Differences:

Memory Allocation:

  • Static members are allocated memory once, shared across all instances of the class.
  • Non-static members are allocated memory for each instance separately.

Access:

  • Static members can be accessed using the class name.
  • Non-static members are accessed through instances of the class.

Initialization:

  • Static members are initialized only once, at the time of class loading.
  • Non-static members are initialized each time an instance is created.

Use Cases:

  • Static members are suitable for constants, utility methods, and shared resources.
  • Non-static members are used for properties and behaviors specific to each instance.

Here's a tabular representation of the differences between static and non-static members in Java:

Feature Static Members Non-Static Members
Memory Allocation Allocated once for the class. Allocated separately for each instance.
Access Accessed using the class name. Accessed through instances.
Initialization Initialized once (at class loading). Initialized each time an instance is created.
Usage Shared among all instances. Specific to each instance.
Keyword Declared with static keyword. No static keyword.
Examples Static variables, static methods, static blocks. Instance variables, instance methods.

Understanding these differences will help you make informed decisions when designing and implementing classes in Java, ensuring that you use static and non-static members appropriately based on the requirements of your application.

Let's create a simple Java program that demonstrates the differences between static and non-static members. We'll create a class called MemberExample with both static and non-static variables, methods, and a main method to showcase their behavior.

File Name: MemberExample.java

Output:

Static block: Executed during class loading.
Static variable: 10
Static method: Accessed using the class name.
Non-static block: Executed during instance creation.
Non-static block: Executed during instance creation.
Instance 1 variable: 5
Instance 2 variable: 8
Non-static method: Accessed through instances.Non-static method: Accessed through instances.

Explanation:

The program defines a class MemberExample with a static variable, a non-static variable, a static block, a non-static block, a static method, and a non-static method.

In the main method, it demonstrates the access and behavior of static and non-static members.

The static block is executed only once during the class loading process, while the non-static block is executed each time an instance of the class is created.

The static method is invoked using the class name, while the non-static method is invoked on instances of the class.

Compile and run this Java program to observe the output, which will help you understand the execution order and behavior of static and non-static members in Java.

Conclusion:

Understanding the distinction between static and non-static members in Java is crucial for effective object-oriented programming. Static members provide shared functionality across all instances, while non-static members allow for unique properties and behaviors for each object. By leveraging these concepts appropriately, developers can design more modular, scalable, and efficient Java applications.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA