Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there an idiom in Java for empty methods which exist to satisfy an interface?
    primarykey
    data
    text
    <p>Let's say I have a class <code>Foo</code> implementing an interface such as <code>MouseListener</code>. The <code>MouseListener</code> interface consists of five methods but I only wish to override one of them (<code>mouseClicked()</code>). Is there a standard, idiomatic way of formatting the other methods?</p> <p>My inclination was to write the following:</p> <pre><code>@Override public void mouseClicked(MouseEvent e) { // (...) &lt;-- actual code here } @Override public void mouseEntered(MouseEvent e) { // Do nothing. Exists to satisfy MouseListener interface. } @Override public void mouseExited(MouseEvent e) { // Do nothing. Exists to satisfy MouseListener interface. } @Override public void mousePressed(MouseEvent e) { // Do nothing. Exists to satisfy MouseListener interface. } @Override public void mouseReleased(MouseEvent e) { // Do nothing. Exists to satisfy MouseListener interface. } </code></pre> <p>I'm a fan of making it explicit that methods are intentionally blank rather than accidentally left so, but I'm not crazy about all the vertical space given up for basically nothing. I've also seen the following format:</p> <pre><code>public void mouseClicked(MouseEvent e) { // (...) &lt;-- actual code here } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} </code></pre> <p>I'm generally OK with this and I understand the author's intent, but it gets really ugly when the (<a href="https://stackoverflow.com/questions/212614/should-a-method-that-implements-an-interface-method-be-annotated-with-override">recommended</a>) <code>@Override</code> annotations are added.</p> <p>I'm not a particularly experienced Java coder so I figured I'd ask if there was a convention. Thoughts?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    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