rspec/rules/S1171/java/rule.adoc

106 lines
2.9 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2024-02-27 16:24:58 +01:00
Non-static initializers, also known as instance initializers, are blocks of code within a class that are executed when an instance of the
2023-05-30 16:06:53 +02:00
class is created. They are executed when an object of the class is created just before the constructor is called.
Non-static initializers are useful when you want to perform some common initialization logic for all objects of a class. They allow you to
initialize instance variables in a concise and centralized manner, without having to repeat the same initialization code in each constructor.
2021-04-28 16:49:39 +02:00
2023-05-30 16:06:53 +02:00
While non-static initializers may have some limited use cases, they are rarely used and can be confusing for most developers because they
only run when new class instances are created.
2023-05-30 16:06:53 +02:00
== How to fix it
2021-04-28 16:49:39 +02:00
2023-05-30 16:06:53 +02:00
Non-static initializers should be refactored into standard constructors or field initializers when possible.
In most cases, the use of constructors, overloaded constructors, or factory methods is preferable for initializing instance variables.
These approaches provide more explicit and controlled initialization, separate concerns, allow for better error handling, and make the code
easier to understand and maintain.
=== Code examples
==== Noncompliant code example
[source,java,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
class MyClass {
private static final Map<String, String> MY_MAP = new HashMap<String, String>() {
{
put("a", "b");
}
2023-05-30 16:06:53 +02:00
}; // Noncompliant - HashMap should be extended only to add behavior, not for initialization
2021-04-28 16:49:39 +02:00
}
----
2023-05-30 16:06:53 +02:00
==== Compliant solution
2021-04-28 16:49:39 +02:00
2023-05-30 16:06:53 +02:00
Using static initialization block:
[source,java,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
class MyClass {
2023-05-30 16:06:53 +02:00
private static final Map<String, String> MY_MAP = new HashMap<>();
2021-04-28 16:49:39 +02:00
static {
2023-05-30 16:06:53 +02:00
MY_MAP.put("a", "b"); // Compliant
}
}
----
or using constructor:
[source,java]
2023-05-30 16:06:53 +02:00
----
class MyClass {
private static final Map<String, String> MY_MAP = new HashMap<>();
public MyClass() {
MY_MAP.put("a", "b"); // Compliant
2021-04-28 16:49:39 +02:00
}
}
----
2023-05-30 16:06:53 +02:00
or using Java 9 `Map.of`:
[source,java]
2021-04-28 16:49:39 +02:00
----
class MyClass {
2023-05-30 16:06:53 +02:00
private static final Map<String, String> MY_MAP = java.util.Map.of("a", "b"); // Compliant
2021-04-28 16:49:39 +02:00
}
----
2023-05-30 16:06:53 +02:00
or using Guava `ImmutableMap.of`:
2021-04-28 16:49:39 +02:00
[source,java]
2021-04-28 16:49:39 +02:00
----
class MyClass {
2023-05-30 16:06:53 +02:00
private static final Map<String, String> MY_MAP = com.google.common.collect.ImmutableMap.of("a", "b"); // Compliant
2021-04-28 16:49:39 +02:00
}
----
2023-05-30 16:06:53 +02:00
== Resources
=== Articles & blog posts
* https://www.baeldung.com/java-static-instance-initializer-blocks[Static vs. Instance Initializer Block in Java]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Move the contents of this initializer to a standard constructor or to field initializers.
'''
== Comments And Links
(visible only on this page)
=== is duplicated by: S3476
=== on 31 Jul 2013, 14:43:50 Freddy Mallet wrote:
Is implemented by \http://jira.codehaus.org/browse/SONARJAVA-260
endif::env-github,rspecator-view[]