rspec/rules/S2325/java/rule.adoc

63 lines
1.4 KiB
Plaintext

== Why is this an issue?
Non-overridable methods (``++private++`` or ``++final++``) that don't access instance data can be ``++static++`` to prevent any misunderstanding about the contract of the method.
=== Noncompliant code example
[source,java]
----
class Utilities {
private static String magicWord = "magic";
private String getMagicWord() { // Noncompliant
return magicWord;
}
private void setMagicWord(String value) { // Noncompliant
magicWord = value;
}
}
----
=== Compliant solution
[source,java]
----
class Utilities {
private static String magicWord = "magic";
private static String getMagicWord() {
return magicWord;
}
private static void setMagicWord(String value) {
magicWord = value;
}
}
----
=== Exceptions
When ``++java.io.Serializable++`` is implemented the following three methods are excluded by the rule:
* ``++private void writeObject(java.io.ObjectOutputStream out) throws IOException;++``
* ``++private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;++``
* ``++private void readObjectNoData() throws ObjectStreamException;++``
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]