Why do we declare a variable or method as static?
Deepank Sharma
Posted on February 24, 2023
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.
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.
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:
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.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.
- 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.
- If a method doesn't modify the state of an object, or not using any instance variables.
- 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.
- 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:
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.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.
Posted on February 24, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.