Javatpoint Logo
Javatpoint Logo

Difference Between Static and non-static in Java

In order to grasp how classes, variables, and methods operate in Java, it is crucial to comprehend the notions of static and non-static. Non-static members are linked to specific class instances, whereas static members are connected to the class. In this section, we will contrast static versus non-static Java components, highlighting their differences and potential applications.

Associated with

Static: Static members (variables and methods) are associated with the class itself rather than with individual instances.

Non-Static: Non-static members are specific to each instance of a class, as they are tied to objects created from the class.

Memory Allocation

Static: Static members are allocated memory only once, at the time of class loading. They are shared among all instances of the class.

Non-Static: Non-static members have memory allocated separately for each instance of the class. Each object has its own copy of non-static members.

Accessing

Static: Static members can be accessed directly using the class name followed by the member name (e.g., ClassName.memberName). They are accessible from anywhere within the program.

Non-Static: Non-static members are accessed using an object reference followed by the member name (e.g., objectReference.memberName). They are specific to a particular instance of the class.

Initialization

Static: Static members are initialized when the class is loaded into memory, typically during program startup. Initialization happens only once.

Non-Static: Non-static members are initialized when each instance of the class is created, usually using the new keyword. Initialization occurs separately for each object.

Scope

Static: Static members have a global scope and can be accessed from anywhere within the program, even without creating an instance of the class.

Non-Static: Non-static members have a local scope and can be accessed only through an instance of the class. They are not accessible without creating an object.

Access to Members

Static: Static members can only access other static members within the same class. They cannot directly access non-static members.

Non-Static: Non-static members can access both static and non-static members within the same class. They have direct access to all members.

Usage

Static: Static members are commonly used for utility methods, constants, or variables that are not specific to individual instances. For example, a Math class containing mathematical functions.

Non-Static: Non-static members are used for instance-specific behavior, as they hold data specific to each object. For example, instance variables that store unique values for each object.

Memory Efficiency

Static: Static members consume memory only once, regardless of the number of instances created. They can be memory-efficient when the same data must be shared across all objects.







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