DEV Community

Deepank Sharma
Deepank Sharma

Posted on

Why do we declare a variable or method as static?

When I started programming, I would often jump onto the implementation and execution of a concept without understanding the intricacies or the need for the same.
In this blog, I'm going to walk you through a basic concept in Java and we will dive deep into it, understanding its necessity.

Before we jump onto the static, let's see how objects and variables are stored in java.

Alt Text

The object's references are stored in a stack and the instances, variables, and methods are stored in heap.

STATIC VARIABLES
In the case of static variables, there is a shared resource for all the references.
Alt Text

So when a variable is shared amongst all the objects or a change in its value impacts all references it should be declared static.
Let's look at a few practical examples to comprehend it better:

  1. Flight ticket counter
    The total number of seats in a flight should be declared static as the number gets updated for every user while booking the seats, every user will see the updated value of available seats.

  2. E-commerce application
    Here the total items in the cart can be made static as we're adding items from multiple sources. When we add an item the total items change to 1 and when another item is added we need to update its value to 2.

STATIC METHODS

When you see a static method in Java code, What do you assume? What reasoning and assumptions a reader makes when he sees a static method? This is important to learn to ensure we are using the static method correctly.

  1. One rule of thumb would be, ask yourself "Does it make sense to call this method even if no object has been created yet?". If yes, then it should be named static.
  2. If a method doesn't modify the state of an object, or not using any instance variables.
  3. A method is a good candidate for being static if it only works on arguments provided to it. e.g. public int factorial(int number){}, this method only operates on the number provided as an argument.
  4. Static methods exist as a single copy for a class while instance methods exist as multiple copies, so as in the static variable here also the particular piece of code is shared by all.

Let's take a look at a few practical examples:

  1. In a car class we have a method:
    double convertMpgToKpl(double Mpg)
    This method is responsible for converting Mpg to Kpl. Now it is possible that no instance of a car is created but somebody wants to use this method, hence it makes sense to make it static.

  2. In an organization we have a method:
    getAllEmployees()
    We have two distinct objects, obj1, obj2. Would there ever be a case when ob1.getAllEmployees and ob2.getAllEmployees yield a different result?
    So why do we need ob1 and ob2? Since the object creation does not yield a different state, it does not make sense to create objects here and hence the method can directly be accessed through the className, hence static.

Top comments (0)