Java Static Methods

Static method in JavaJava static methods are one of the most important language instrument.

I’ll explain what’s static function in Java, how to make it and what is its purpose.

I’ll describe what is the difference between static and non-static methods and why main() method in Java is static.

To define static method you should add “static” keyword to the signature.

Let’s take a look at public static method example in Java:

Method invocation looks like this:

You can define as many static methods as you want.

Since Java 8 you can define static methods in the interface as well.

Looks easy, but you can ask me:

“Why should I use it?”.

Use of Static Methods in Java: 5 Criteria

Usually, I’m following a rule: if a method can be static – it should be static.

But in general, I defined 5 cases of use of static in java:

  • The method does not suppose class instance field access.
  • The operation is independent of the class instance.
  • Your piece of code can be shared between all class instances.
  • You don’t need to override method behavior in the child class.
  • You’re writing utility classes like StringUtils in my example.

So the main purpose of static methods is to reduce memory usage because you don’t need to create a new object instance.

Tweet 5 Reasons to Use Static MethodsClick To Tweet

What is a Difference Between Static and Nonstatic Methods?

I know 2 main differences:

  • A static method belongs to the class and nonstatic to the class instance, so you can invoke 1st one without building a new object.
  • A static method can access only static class members (e.g. static variables), nonstatic methods can access both.
Tweet Difference Between Static & Nonstatic MethodsClick To Tweet

Сan I Override Static Method in Java?

Nope. You can’t override static methods.

As I mentioned before it belongs to a class.

So if you’ll define a static method with the same signature in a child class it’ll be a new static method that belongs to a child class.

Let’s imagine you have 2 classes (Parent and Child) with a same static method hello().

Child extends Parent.

Example:

Looks like Child.hello() overrides Parent.hello() method.

But let’s take a look what happens if you invoke these methods in different ways.

Output:

1 and 2 method calls are something usual and expected.

3 and 4 calls are more interesting.

It’s the same instance of a Child class, but different methods have been invoked.

In the 3rd call, instance points to Child class and invokes Child.hello method().

In the 4th call, instance point to Parent class and invokes Parent.hello().

But if parent static method has final modifier child class cannot create its own static method with the same signature because of a compile error.

Tweet Can I Override Static Methods in JavaClick To Tweet

Why main() Method is Static?

public static void main(String[] args) is an entry point of Java program.

It’s defined as a static, but what is a purpose?

The reason is:

JVM can invoke it without creating an instance of a class.

That means you should not care about creating an instance of a parent class and decide what constructor to use.

It prevents ambiguity.

Tweet Why Main() Method is Static in JavaClick To Tweet

Do you have any questions about static methods in Java? Feel free to ask.

1 Star2 Stars3 Stars4 Stars5 Stars
Loading...
Scroll Up