2

Is there a runtime advantage to importing static methods vs calling the static class.method() as needed?

Example...

package com;

public class TestMain {
    public static void main(String[] args) {
        TestOne firstTester = new TestOne();
        TestTwo secondTester = new TestTwo();

        firstTester.setVars();
        firstTester.setVarSum();
        firstTester.printVarSum();

        secondTester.setVars();
        secondTester.setVarSum();
        secondTester.printVarSum();
    }
}

package com;

public abstract class TestSubMethods {
    public static int getMethodOne(){return 1;}
    public static int getMethodTwo(){return 2;}
    public static int getMethodThree(){return 3;}
    public static int getMethodFour(){return 4;}
}

-------------------------------------------------------------------

package com;

public class TestOne {
    private int varOne;
    private int varTwo;
    private int varThree;
    private int varFour;
    private int varSum;

    public void setVars() {
        this.varOne = TestSubMethods.getMethodOne();
        this.varTwo = TestSubMethods.getMethodTwo();
        this.varThree = TestSubMethods.getMethodThree();
        this.varFour = TestSubMethods.getMethodFour();
    }

    public void setVarSum() {
        this.varSum = this.varOne + this.varTwo + this.varThree + this.varFour;
    }

    public void printVarSum() {
        System.out.println("varSum = " + this.varSum);
    }
}

-----vs-----

package com;

import static com.TestSubMethods.*;

public class TestTwo {
    private int varOne;
    private int varTwo;
    private int varThree;
    private int varFour;
    private int varSum;

    public void setVars() {
        this.varOne = getMethodOne();
        this.varTwo = getMethodTwo();
        this.varThree = getMethodThree();
        this.varFour = getMethodFour();
    }

    public void setVarSum() {
        this.varSum = this.varOne + this.varTwo + this.varThree + this.varFour;
    }

    public void printVarSum() {
        System.out.println("varSum = " + this.varSum);
    }
}

Between "TestOne" and "TestTwo" Is there a preferred style? Is one a faster runtime than the other? I was going to use "extends TestSubMethods", but it was recommended I don't do this.

Fiddle Freak
  • 1,923
  • 5
  • 43
  • 83
  • 2
    No, none at all. The `import` is just a name convenience for the programmer writing the code. It has no effect on runtime. – markspace May 11 '18 at 03:57
  • That's good to know thx, so I guess it just comes down to style? – Fiddle Freak May 11 '18 at 03:57
  • Yes, basically it's style. Occasionally you might have two types with the same name, for example, `Response` or something and you'll have to use the fully qualified name for one. Personally I think static imports are going out of style (import the class then call `ClassName.staticMethod`) but it's not a big deal either way. – markspace May 11 '18 at 03:59
  • This may be a dupe: https://stackoverflow.com/questions/162187/what-does-the-static-modifier-after-import-mean?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – markspace May 11 '18 at 04:01
  • Yeah, I don't think this question is a dup, but the guy's answer in that other question certainly is a valid answer for my question. I think the guy was asking the difference between `import static ClassName.*` and `import static ClassName.methodName`. Where as mine is `import static ClassName.*` vs being in a function called as `ClassName.methodName()`. – Fiddle Freak May 11 '18 at 04:17
  • I think for now I like the `ClassName.staticMethod()` better due to easier code tracing knowing directly where the called methods are coming from. So I'll use that for now. If it gets to be too many... I may just use the import static. Thanks again for the answer and I'll leave it up to the experts to close this question or not. – Fiddle Freak May 11 '18 at 04:21
  • Either way you can just ctrl-click it in your IDE to go to it. – David Conrad May 11 '18 at 04:40

1 Answers1

2

Only difference I feel is static import decreases readability.
When static import is used, one cannot tell where this method exists.

public void setVars() {
    this.varOne = getMethodOne(); // static method from current class
    this.varTwo = getMethodTwo(); // static method imported from other class
}

But at the same time while writing test cases,

Assert.assertEquals(1, 1); // not preferred
assertEquals(1, 1); // prferable

That's because we are habitual of it, and we don't get confuse where assertEquals exists.

Hemant Patel
  • 3,160
  • 1
  • 20
  • 29