Popular
Data Science
Technology
Finance
Management
Future Tech
Java has three kinds of variables: instance, local, and static. The instance variables belong to the individual objects, while the static ones are attached to the class. All the class objects will share a static variable and contain one single copy, no matter how many instances you may create. Let’s explore static variables in Java in detail!
A Java static variable is shared among all the class instances. Whereas instance variables are unique to every object, a static variable does not belong to any particular instance but rather to the class itself. Changes made to a static variable by any instance are reflected across all class instances. Static variables store data that should be common to all class objects rather than unique to each instance.
Java Developer also used a static variable as a constant with the final keyword. Constants must be initialized during the declaration. It means the declaration cannot be changed after being assigned.
The following program demonstrates the Initialization of static final variables in Java language.
Program
Output
Static variables in Java have several distinct properties that differentiate them from instance variables. Here are the key properties.
The static variable and method are stored in the heap memory. It is a specific part of the heap memory dedicated to storing class-level information. When a class is loaded by the JVM, its static variables are allocated memory in this method area, which is shared across all class instances. This ensures that static variables are initialized once and accessed directly through the class name rather than individual object instances.
We can declare a static variable through the use of a static keyword. Static variables belong to the class rather than to any specific class instance. This means that all instances of the class share the same static variable.
Declaration of Static Variables
The following program demonstrates the static variable in Java.
Program
Output
In Java, a static variable is associated with a class rather than a class instance. There is no need to create an object to access the static variables because static variables are the class variables, and static variables are shared among all the class instances. Simply, we can access the static variable using the class name. (eg, ‘ClassName.variableName’).
The following program demonstrates the accessibility of static variables.
Program
Output
The ‘static’ keyword in Java is primarily used for memory management and to define class-level elements.
Syntax of Static Variable in Java
static datatype variableName = Value;
The following program demonstrates the static variable in Java.
Program
Output
An example of a static variable in real life is the use of a static counter in Java to define how many times an object was instantiated from a class. The situation occurs whenever the course of the execution of a program is to be observed in terms of the number of instances of a given class.
The following program demonstrates a real-life example in Java.
Program
Output
Static variables in Java are an essential aspect of class-level data management. They can save on memory, as they are shared across all instances of the class, but allow easy access to common values. Thus, a Java programmer should understand how to declare, initialize, and use static variables. With their ability to maintain a single copy of a variable for all objects, static variables help in scenarios where a standard resource or configuration is needed. This concept is fundamental for any Java developer looking to write clean and optimized source code.
The DevOps Playbook
Simplify deployment with Docker containers.
Streamline development with modern practices.
Enhance efficiency with automated workflows.
Popular
Data Science
Technology
Finance
Management
Future Tech
Accelerator Program in Business Analytics & Data Science
Integrated Program in Data Science, AI and ML
Certificate Program in Full Stack Development with Specialization for Web and Mobile
Certificate Program in DevOps and Cloud Engineering
Certificate Program in Application Development
Certificate Program in Cybersecurity Essentials & Risk Assessment
Integrated Program in Finance and Financial Technologies
Certificate Program in Financial Analysis, Valuation and Risk Management
© 2024 Hero Vired. All rights reserved