Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Wiki Markup
An alternative way of clearing the {{vector}} is to use the {{vector.clear()}} method. Likewise,  if a range of elements hashave to be releasedremoved from the {{vector}}, {{vector.subList(fromIndex, toIndex).clear()}} can be used. In the context of this compliant solution, the {{fromIndex}} and the {{toIndex}} can both be {{0}} as the {{count}} variable is {{1}} on each iteration. \[[API 06|AA. Java References#API 06]\]

...

Code Block
bgColor#FFCCCC
public class BadScope {
  private HashMap<Integer,String> hm = new HashMap<Integer, String>();
  
  private void doSomething() {
    hm.put(1, "java");  // hm is used only here
  }
}

...

Code Block
bgColor#ccccff
Reader reader = new Reader();
button.addActionListener(reader);
try {
  reader.readSomething();
} catch (IOException e) { 
  // Handle exception 
} finally {
  button.removeActionListener(reader);  // Always gets executed
}

Noncompliant Code Example

...

Code Block
bgColor#FFCCCC
class HashMemLeak {
  private Map<SSLSocket, InetAddress> m = Collections.synchronizedMap(new HashMap<SSLSocket, InetAddress>());
  public void storeTempConnection(SSLSocket sock, InetAddress ip) {
	    m.put(sock, ip);  
  }
  public void removeTempConnection(SSLSocket sock) {
	    m.remove(sock);  
  }	
}

Compliant Solution

...