"equals(Object obj)" and "hashCode()" should be overridden in pairsAccording to the Java Language Specification, there is a contract between If two objects are equal according to the In order to comply with this contract, those methods should be either both inherited, or both overridden. Noncompliant Code Exampleclass MyClass { // Non-Compliant - should also override "hashCode()" @Override public boolean equals(Object obj) { /* ... */ }} Compliant Solutionclass MyClass { // Compliant @Override public boolean equals(Object obj) { /* ... */ } @Override public int hashCode() { /* ... */ }} | ||
"equals(Object obj)" should be overridden along with the "compareTo(T obj)" methodAccording to the Java It is strongly recommended, but not strictly required that If this rule is violated, weird and unpredictable failures can occur. For example, in Java 5 the Noncompliant Code Examplepublic class Foo implements Comparable Compliant Solutionpublic class Foo implements Comparable | ||
Execution of the Garbage Collector should be triggered only by the JVMCalling
An application relying on those unpredictable methods is also unpredictable and therefore broken. The task of running the garbage collector should be left exclusively to the JVM. | ||
Loop counters should not be assigned to from within the loop bodyLoop counters should not be modified in the body of the loop. However other loop control variables representing logical values may be modified in the loop, for example a flag to indicate that something has been completed, which is then tested in the for statement. The following code: String[] names = new String[]{ "Jack", "Jim", null, "John" };for (int i = 0; i < names.length; i++) { if (names[i] == null) { i = names.length; // Non-Compliant } else { System.out.println(names[i]); }} should be refactored into: String[] names = new String[]{ "Jack", "Jim", null, "John" };for (String name: names) { if (name == null) { break; // Compliant } System.out.println(name);} | ||
Nested blocks of code should not be left emptyMost of the time a block of code is empty when a piece of code is really missing. So such empty block must be either filled or removed. When a block contains a comment, this block is not considered to be empty. The following code snippet illustrates this rule: void doSomething() { for (int i = 0; i < 42; i++) // Non-Compliant { } for (int i = 0; i < 42; i++); // Compliant if (myVar == 4) // Compliant - contains a comment { // Do nothing because of X and Y } else // Compliant { doSomething(); } try // Non-Compliant { } catch (Exception e) // Compliant { // Ignore }} | ||
Return statements should not occur in finally blocksReturning from a The following code snippet illustrates this rule. The developer expects to get "ERROR" in the console whereas "OK" is displayed. public static void main(String[] args) { try { doSomethingWhichThrowsException(); System.out.println("OK"); } catch (RuntimeException e) { System.out.println("ERROR"); }}public static void doSomethingWhichThrowsException() { try { throw new RuntimeException(); } finally { /* ... */ return; // Non-Compliant - prevents the RuntimeException from being propagated }} | ||
Strings should be compared using equals()String literals, just like any other The following code: if (variable == "foo") { /* ... */ } // Non-Compliantif (variable != "foo") { /* ... */ } // Non-Compliant should be refactored into: if ("foo".equals(variable)) { /* ... */ } // Compliantif (!"foo".equals(variable)) { /* ... */ } // Compliant | ||
super.finalize() should be called at the end of Object.finalize() implementationsOverriding the The following code snippet illustrates this rule: protected void finalize() { // Non-Compliant releaseSomeResources();}protected void finalize() { super.finalize(); // Non-Compliant releaseSomeResources();}protected void finalize() { releaseSomeResources(); super.finalize(); // Compliant} |