Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>More often than not, I see them at the top of the class.</p> <pre><code>public class MyClass { private int count; private String name; public MyClass() { // Methods go below. } } </code></pre> <p>I've always done this naturally, but I think it's better that you put them <em>all in the same place</em>. If you've got them dotted around, it just becomes confusing. For example:</p> <pre><code>public void doSomething() { x = x * 6; } public void anotherMethod() { y = y * 4; } int x = 0; public void anotherMethodEntirely() { z = z * 10; } double y = 0; float z = 9; </code></pre> <p>You see how it's just counter productive? You want to be able to read the method, and know everything about the nature of the variables involved. Not have to keep firing around the code hunting down members.</p> <p>If they're all in the same place, however:</p> <pre><code>public void doSomething() { x = x * 6; } public void anotherMethod() { y = y * 4; } public void anotherMethodEntirely() { z = z * 10; } int x = 0; double y = 0; float z = 9; </code></pre> <p>All you need to do is go to the bottom (in this instance) to see the variable types.</p> <p><b> Taken from the Java Code Convention </b></p> <blockquote> <p>Put declarations only at the beginning of blocks. (A block is any code surrounded by curly braces "{" and "}".) Don't wait to declare variables until their first use; it can confuse the unwary programmer and hamper code portability within the scope.</p> </blockquote> <p>Full article <a href="http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141270.html#381" rel="nofollow">here</a></p>
 

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