Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In java static methods are not <b>inherited</b> (or the right word is <b>overridden</b>) but they can be <b>hidden</b>.</p> <p>The big different here is that they are not subjected to polymorphism as object method are.</p> <pre><code>public class C1 { static public void M1() { System.out.println("C1.M1()."); } static public void main(String ... Args) { M1(); } } public class C2 extends C1 { static public void M1() { System.out.println("C2.M1()."); } static public void main(String ... Args) { M1(); C1.main(Args); } }</code></pre> <p>When run <code>C2.main(null)</code>, you will get:</p> <pre><code>C2.M1(). C1.M1().</code></pre> <p>As you can see,</p> <p>calling <code>M1()</code> in C1.main(...) refer to M1 of C1 and</p> <p>calling <code>M1()</code> in C2.main(...) refer to M1 of C2.</p> <p>The invocation of M1 (with out any prefix, see the first line of each <code>main()</code>), are not subjected to polymorphism as M1 in C1 does not get overrided by C2.</p> <p>But calling from C2 refer to M1 of C2 as M1 of C2 is <b>hide</b> the one in C1.</p> <p>Read more <a href="http://java.sun.com/docs/books/tutorial/java/IandI/override.html" rel="noreferrer">here</a>.</p> <p><b>EDIT:</b> I've just re-read your question and just see the part about "good programming practise".</p> <p>As I said, static method are not inherited but hidden so they are as good as different method.</p> <p>From a code's point of view, they are completely different methods.</p> <p>Let say.</p> <pre><code>C1 has static method M1. C2 extends C1 and has static method M1. C3 extends C2.</code></pre> <p>When call M1 (without prefix) from C1, you call C1.M1(). When call M1 (without prefix) from C2, you call C2.M1(). // derive but get hidden When call M1 (without prefix) from C3, you call C3.M1(). // derive and no hidden</p> <p>To specify which method, use class name like <code>C1.M1()</code>, <code>C2.M1()</code> and <code>C3.M1()</code> (this will called <code>C2.M1()</code>).</p> <p>So this implementation allows static method to be reimplemented but only as a different method not as an overridden (or replacement) method.</p> <p>Therefore, this usually no different from let say naming them differently like: <code>C1_M()</code> and <code>C2_M()</code>.</p> <p>So you may ask, why bother having this feature? I don't really know. Perhaps allows a more flexible naming to method.</p> <p>But there is usage (that might or might not be intented) that I used is polymorphism via reflection.</p> <p>Since you can get and call a method by name using reflection, allowsing them to have a same name will enable polymorphism when do via reflection.</p> <p>For example (rought code, may not run):</p> <pre><code>String aClsName = "C1"; // "C2"; Class aCls = Class.forName(aClsName); Method aMth = aCls.getMethod("M1"); // M1 of C1 or C2 depends on the class name. aMth.invoke(null); </code></pre> <p>OR</p> <pre><code>Object aObj = new C1(); // new C2(); Class aCls = aObj.getClass(); Method aMth = aCls.getMethod("M1"); // M1 of C1 or C2 depends on the class of the object. aMth.invoke(null); </code></pre> <p>When think about it, I think Java has use this too (like, <code>writeObject(...)</code> for serialization) so it may be intented.</p> <p>So to conclude, hiding static method is not a good programming practice (Eclipse also recommend against it) but it can be useful in two cases, (1) to name the method exact what it suppose to do and (2) to polymorph it using reflection.</p> <p>Hope this helps.</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. 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