Sitemap

Breaking It Down Bollywood Style: Static vs. Non-Static in Java

3 min readOct 18, 2023

--

Namaste, Java enthusiasts! Today, we’re going to unravel the mystic magic of “static” and “non-static” in Java, Bollywood style. Picture this — Java is the leading star, and “static” and “non-static” are the two entertaining co-stars who add the drama to our code.

What’s the Deal with Static, Yaar?

Static Variables: Like an Evergreen Bollywood Hit

Static variables, my friend, are like Bollywood songs that never get old. They belong to the class itself. It’s like Amitabh Bachchan’s charisma — you don’t need multiple Amitabh’s; one Big B is enough for all. These are declared with the static keyword.

Static Methods: The Bollywood Blockbusters

Static methods are like those timeless Bollywood blockbusters that everyone talks about. They’re associated with the class, not with instances. Think of them as Shah Rukh Khan — they’re the class act, and you can call them without creating an object.

When to Use Static — The “Kuch Kuch Hota Hai” Moment

When should you use static? Well, picture this:

Static is your Raj for maintaining a common state or behavior across all instances, just like SRK’s charm unites everyone.

  • Constants: Constants like the value of Dharmendra’s smile, they remain constant throughout.
  • Utility Methods: They’re the Aamir Khan of methods, versatile and useful for everyone.
  • Counters: Remember Hrithik Roshan’s dance moves? They’re unique and counted per dance step.

Meet Non-Static — The K3G of Java

Non-Static Variables: The Bollywood Wardrobe

Non-static variables are like your wardrobe. Each instance gets its own set of clothes, and changes to one don’t affect the others. No sharing here! They’re declared without the static keyword.

Non-Static Methods: The Star Performers

Non-static methods are like our Bollywood stars — they perform on the stage of individual objects. Each one has its own charm. They can access non-static variables and dance to their tune.

When to Use Non-Static — The “Zindagi Na Milegi Dobara” Rule

When should you use non-static? Well, it’s like planning a road trip with your buddies:

Get Dinesh Borude’s stories in your inbox

Join Medium for free to get updates from this writer.

Non-static is your group planning, specific to the gang:

  • Object-Specific Data: Think of it as your choice of vehicle — unique for each friend.
  • Instance Methods: Like the music preferences — tailored for your gang’s taste.
  • Encapsulation: Keep the secrets within the group; don’t spill the beans to the world.

The Bollywood Drama Unfolds

When you declare something as static, it’s like featuring it in every scene of the movie. The spotlight is always on, shared by all. Non-static, on the other hand, is the star’s solo performance. One actor, one stage.

Lights, Camera, Memory!

Static elements take up memory just once, Bollywood’s budget style. Non-static elements, however, demand their own space for every star in the cast. It’s like renting separate vanity vans — one for every hero or heroine.

How to Play the Bollywood Game

Using Static:

  • For evergreen constants that are known to every fan.
  • For utility methods that are like the iconic “Swag se Swagat.”
  • Counters, to keep track of how many “awards” each actor has won.

Using Non-Static:

  • For unique data specific to each “character.”
  • For individual methods, like the signature dance moves of each star.
  • To keep the Bollywood secrets in the family, not for public gossip.

In conclusion, static and non-static are like the two main leads in a Bollywood blockbuster — they play their roles and create a blockbuster hit together. Choose them wisely, my friends. Static is the Dhoom, always in the limelight. Non-static is the thoughtful Swades, where each character has its unique journey. Use them to create your own Bollywood code drama and, remember, there’s always room for both in this masaledar world of Java programming!

--

--

Dinesh Borude
Dinesh Borude

Written by Dinesh Borude

I'm Dinesh, a tech enthusiast. Join my blog for tales on testing, cybersecurity, AI, automation, and the wonders of Flutter!