43 lines
1.1 KiB
Plaintext
43 lines
1.1 KiB
Plaintext
![]() |
<code>private</code> methods that don't access instance data can be <code>static</code> to prevent any misunderstanding about the contract of the method.
|
||
|
|
||
|
== Noncompliant Code Example
|
||
|
|
||
|
----
|
||
|
class Utilities {
|
||
|
private static String magicWord = "magic";
|
||
|
|
||
|
private String getMagicWord() { // Noncompliant
|
||
|
return magicWord;
|
||
|
}
|
||
|
|
||
|
private void setMagicWord(String value) { // Noncompliant
|
||
|
magicWord = value;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
----
|
||
|
|
||
|
== Compliant Solution
|
||
|
|
||
|
----
|
||
|
class Utilities {
|
||
|
private static String magicWord = "magic";
|
||
|
|
||
|
private static String getMagicWord() {
|
||
|
return magicWord;
|
||
|
}
|
||
|
|
||
|
private static void setMagicWord(String value) {
|
||
|
magicWord = value;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
----
|
||
|
|
||
|
== Exceptions
|
||
|
|
||
|
When <code>java.io.Serializable</code> is implemented the following three methods are excluded by the rule:
|
||
|
* <code>private void writeObject(java.io.ObjectOutputStream out) throws IOException;</code>
|
||
|
* <code>private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;</code>
|
||
|
* <code>private void readObjectNoData() throws ObjectStreamException;</code>
|