Home > Software design >  Does calling a static void method with empty body do anything?
Does calling a static void method with empty body do anything?

Time:05-21

There is a Java class with just an static method with an empty body.

package com;

public class MyClass {
    public static void EmptyMethod() {
    }
}

would calling this method in another class do anything, for example like

public class AnotherClass
{
   public final void someMethod()
   {
      int i = 10;
      MyClass.EmptyMethod();
      i = 12;
   }
}

?

I saw it was there being called in various methods inside an Android project and I could not understand why.

CodePudding user response:

No, it has no effect. Only, if you are really crazy about performance, that method call will cost a minimal amount of "time".

CodePudding user response:

I would say that it does indeed have an effect and here is why.

With the code call to the static method inside the someMethod() - the program will still call the EmptyMethod() - this is executed, taking a minimal amount of time like @David Weber says.

If there was 'no effect' somehow Java would determine that the static method is empty, so avoid doing anything with it, as if the line of code calling the method didn't even exist. This is not what happens.

Therefore - it does have an effect - but it clearly wouldn't functionally change the program in this example!

  • Related