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.

7
  • 2
    No, none at all. The import is just a name convenience for the programmer writing the code. It has no effect on runtime. Commented May 11, 2018 at 3:57
  • That's good to know thx, so I guess it just comes down to style? Commented May 11, 2018 at 3: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. Commented May 11, 2018 at 3:59
  • This may be a dupe: stackoverflow.com/questions/162187/… Commented May 11, 2018 at 4: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(). Commented May 11, 2018 at 4:17

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was thinking the exact same thing. Thanks for the response :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.