Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It might result in slightly smaller bytecode, since the static methods won't get access to <code>this</code>. I don't think it makes any difference in speed (and if it did, it would probably be too small to make a difference overall).</p> <p>I would make them static, since I generally do so if at all possible. But that's just me.</p> <hr> <p><strong>EDIT:</strong> This answer keeps getting downvoted, possibly because of the unsubstantiated assertion about bytecode size. So I will actually run a test.</p> <pre><code>class TestBytecodeSize { private void doSomething(int arg) { } private static void doSomethingStatic(int arg) { } public static void main(String[] args) { // do it twice both ways doSomethingStatic(0); doSomethingStatic(0); TestBytecodeSize t = new TestBytecodeSize(); t.doSomething(0); t.doSomething(0); } } </code></pre> <p>Bytecode (retrieved with <code>javap -c -private TestBytecodeSize</code>):</p> <pre><code>Compiled from "TestBytecodeSize.java" class TestBytecodeSize extends java.lang.Object{ TestBytecodeSize(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."&lt;init&gt;":()V 4: return private void doSomething(int); Code: 0: return private static void doSomethingStatic(int); Code: 0: return public static void main(java.lang.String[]); Code: 0: iconst_0 1: invokestatic #2; //Method doSomethingStatic:(I)V 4: iconst_0 5: invokestatic #2; //Method doSomethingStatic:(I)V 8: new #3; //class TestBytecodeSize 11: dup 12: invokespecial #4; //Method "&lt;init&gt;":()V 15: astore_1 16: aload_1 17: iconst_0 18: invokespecial #5; //Method doSomething:(I)V 21: aload_1 22: iconst_0 23: invokespecial #5; //Method doSomething:(I)V 26: return } </code></pre> <p>Invoking the static method takes two bytecodes (byteops?): <code>iconst_0</code> (for the argument) and <code>invokestatic</code>.<br> Invoking the non-static method takes three: <code>aload_1</code> (for the <code>TestBytecodeSize</code> object, I suppose), <code>iconst_0</code> (for the argument), and <code>invokespecial</code>. (Note that if these hadn't been private methods, it would have been <code>invokevirtual</code> instead of <code>invokespecial</code>; see <a href="http://java.sun.com/docs/books/jvms/second_edition/html/Compiling.doc.html#14787" rel="noreferrer">JLS §7.7 Invoking Methods</a>.)</p> <p>Now, as I said, I don't expect there to be any great difference in performance between these two, other than the fact that <code>invokestatic</code> requires one fewer bytecode. <code>invokestatic</code> and <code>invokespecial</code> should both be slightly faster than <code>invokevirtual</code>, since they both use static binding instead of dynamic, but I have no idea if either is faster than the other. I can't find any good references either. The closest I can find is <a href="http://www.javaworld.com/javaworld/jw-06-1997/jw-06-hood.html" rel="noreferrer">this 1997 JavaWorld article</a>, which basically restates what I just said:</p> <blockquote> <p>The fastest instructions will most likely be <code>invokespecial</code> and <code>invokestatic</code>, because methods invoked by these instructions are statically bound. When the JVM resolves the symbolic reference for these instructions and replaces it with a direct reference, that direct reference probably will include a pointer to the actual bytecodes.</p> </blockquote> <p>But many things have changed since 1997.</p> <p>So in conclusion... I guess I'm still sticking with what I said before. Speed shouldn't be the reason to choose one over the other, since it would be a micro-optimization at best.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload