DEV Community

Bola Adebesin
Bola Adebesin

Posted on

1 1

Static Variables and Methods (Java)

Variables

Static variables are tied to the Class itself. Each object of a class shares the static variable.

Non-static variable (instance variables) are tied to the object. In the example below, every object of the Cat class has its own name variable. However, every object shares the catCount variable.

When you see the static keyword in front of a variable name think of a shared variable.

Class Cat 
{
String name; // Non-static variable (each object has it's own copy) 
static int catCount; //Static variable (each object of the class Cat shares this variable

Cat(String name){
this.name = name; // Non-static variables can be referenced with "this" keyword 
Cat.catCount++; // Static variables are referenced using the class name 
}
Enter fullscreen mode Exit fullscreen mode

Methods

Java methods are divided into two categories. Instance methods and Static methods.

Instance Methods

These methods are called on an object and they have access to an objects data (non-static variables). This makes sense. Since an object is an instance of a class, an instance method works on objects.

The reason that instance methods are able to access an objects data is because they have a reference to the object. That reference is stored in the keyword "this". "this" holds a reference that tells us where the object we are referencing is. Without "this" we can't refer to the object.

When you call an instance method, the object that you called the method on is actually passed along to that method as the first argument, we just don't see it.
What we see:

Cat sally = new Cat(); 
String name = sally.getName(); 
Enter fullscreen mode Exit fullscreen mode

What is actually happening:

Cat sally = new Cat(); 
String name = Cat.getName(sally); 
Enter fullscreen mode Exit fullscreen mode

The object sally is being passed into the getName method. Then inside the method, the object sally will be referred to as "this".

Static Methods

These methods do not have access to an objects data. Since they don't have an object reference. Static methods don't have "this". Instead a null value is passed to static methods. But, static methods can reference the static variables of the class and other static methods.

Static methods allow us to call a method before we create any objects because they are tied to the class not the object. This is how the main() method works for example.

Neon image

Serverless Postgres in 300ms (!)

10 free databases with autoscaling, scale-to-zero, and read replicas. Start building without infrastructure headaches. No credit card needed.

Try for Free →

Top comments (0)

Image of Stellar post

🚀 Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay