Static Variables, Constants, and Methods
Tags: java, programming, learning, beginners
Published: 2024-06-03 18:44:57

A static variable is shared by all objects of the class. A static method cannot access instance members of the class. The data field **radius** in the circle class is known as an _instance variable_. An instance variable is tied to a specific instance of the class; it is not shared among objects of the same class. For example, suppose that you create the following objects:

`Circle circle1 = new Circle();
Circle circle2 = new Circle(5);`

The **radius** in **circle1** is independent of the **radius** in **circle2** and is stored in a different memory location. Changes made to **circle1**’s **radius** do not affect **circle2**’s **radius**, and vice versa.

If you want all the instances of a class to share data, use _static variables_, also known as _class variables_. Static variables store values for the variables in a common memory location. Because of this common location, if one object changes the value of a static variable, all objects of the same class are affected. Java supports static methods as well as static variables. _Static methods_ can be called without creating an instance of the class.

Let’s modify the **Circle** class by adding a static variable **numberOfObjects** to count the number of circle objects created. When the first object of this class is created, **numberOfObjects** is **1**. When the second object is created, **numberOfObjects** becomes **2**. The UML of the new circle class is shown in Figure below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ptkughn14gy4jbxcjtp.png)

The **Circle** class defines the instance variable **radius** and the static variable **numberOfObjects**, the instance methods **getRadius**, **setRadius**, and **getArea**, and the static method **getNumberOfObjects**. (Note that static variables and methods are underlined in the UML class diagram.)

To declare a static variable or define a **static** method, put the modifier static in the variable or method declaration. The static variable **numberOfObjects** and the static method **getNumberOfObjects()** can be declared as follows:

`static int numberOfObjects;

static int getNumberObjects() {
return numberOfObjects;
}`

Constants in a class are shared by all objects of the class. Thus, constants should be declared as **final static**. For example, the constant **PI** in the **Math** class is defined as:

`final static double PI = 3.14159265358979323846;`

The new circle class, named **CircleWithStaticMembers**, is defined below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gxcxr4583jdv7fve5xku.png)

Method **getNumberOfObjects()** in **CircleWithStaticMembers** is a static method. All the methods in the **Math** class are static. The **main** method is static, too. Instance methods (e.g., **getArea()**) and instance data (e.g., **radius**) belong to instances and can be used only after the instances are created. They are accessed via a reference variable. Static methods (e.g., **getNumberOfObjects()**) and static data (e.g., **numberOfObjects**) can be accessed from a reference variable or from their class name.

The program below demonstrates how to use instance and static variables and methods and illustrates the effects of using them.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0616t9hiebum5m05ywao.png)

`Before creating objects
The number of Circle objects is 0
After creating c1
c1: radius (1.0) and number of Circle objects (1)
After creating c2 and modifying c1
c1: radius (9.0) and number of Circle objects (2)
c2: radius (5.0) and number of Circle objects (2)`

When you compile **TestCircleWithStaticMembers.java**, the Java compiler automatically compiles **CircleWithStaticMembers.java** if it has not been compiled since the last change.

Static variables and methods can be accessed without creating objects. Line 7 displays the number of objects, which is **0**, since no objects have been created.

The **main** method creates two circles, **c1** and **c2** (lines 10, 17). The instance variable **radius** in **c1** is modified to become **9** (line 20). This change does not affect the instance variable **radius** in **c2**, since these two instance variables are independent. The static variable **numberOfObjects** becomes **1** after **c1** is created (line 10), and it becomes **2** after **c2** is created (line 17).

Note that **PI** is a constant defined in **Math**, and **Math.PI** references the constant. **c1.numberOfObjects** (line 24) and **c2.numberOfObjects** (line 25) are better replaced by **CircleWithStaticMembers.numberOfObjects**. This improves readability, because other programmers can easily recognize the static variable. You can also replace **CircleWithStaticMembers.numberOfObjects** with **CircleWithStaticMembers.getNumberOfObjects()**.

Use **ClassName.methodName(arguments)** to invoke a static method and
**ClassName.staticVariable** to access a static variable. This improves readability, because this makes the static method and data easy to spot.

An instance method can invoke an instance or static method and access an instance or static data field. A static method can invoke a static method and access a static data field. However, a static method cannot invoke an instance method or access an instance data field, since static methods and static data fields don’t belong to a particular object. The relationship between static and instance members is summarized in the following diagram:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53crf3mcl5o20vtse87h.png)

For example, the following code is wrong.

```
  public class A {
  int i = 5;
  static int k = 2;
 
  public static void main(String[] args) {
  int j = i; // Wrong because i is an instance variable
  m1(); // Wrong because m1() is an instance method
  }
 
 public void m1() {
 // Correct since instance and static variables and methods
 // can be used in an instance method
 i = i + k + m2(i, k);
 }

 public static int m2(int i, int j) {
 return (int)(Math.pow(i, j));
 }
 }
```

Note that if you replace the preceding code with the following new code, the program would be fine, because the instance data field **i** and method **m1** are now accessed from an object **a** (lines 7–8):

```
  public class A {
  int i = 5;
  static int k = 2;
 
  public static void main(String[] args) {
  A a = new A();
  int j = a.i; // OK, a.i accesses the object's instance variable
  a.m1(); // OK. a.m1() invokes the object's instance method
  }

 public void m1() {
 i = i + k + m2(i, k);
 }

 public static int m2(int i, int j) {
 return (int)(Math.pow(i, j));
 }
 }
```

How do you decide whether a variable or a method should be an instance one or a static one? A variable or a method that is dependent on a specific instance of the class should be an instance variable or method. A variable or a method that is not dependent on a specific instance of the class should be a static variable or method. For example, every circle has its own radius, so the radius is dependent on a specific circle. Therefore, **radius** is an instance variable of the **Circle** class. Since the **getArea** method is dependent on a specific circle, it is an instance method. None of the methods in the **Math** class, such as **random**, **pow**, **sin**, and **cos**, is dependent on a specific instance. Therefore, these methods are static methods. The **main** method is static and can be invoked directly from a class.

It is a common design error to define an instance method that should have been defined as static. For example, the method **factorial(int n)** should be defined as static, as shown next, because it is independent of any specific instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ftsm45d72fuoj19reye.png)