2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-07-05 15:32:49 +02:00
When an object is marked as `static`, it means that it belongs to the class rather than any class instance.
This means there is only one copy of the static object in memory, regardless of how many class instances are created.
Static objects are shared among all instances of the class and can be accessed using the class name rather than an instance of the class.
2021-04-28 16:49:39 +02:00
2023-07-05 15:32:49 +02:00
A data type is considered thread-safe if it can be used correctly by multiple threads, regardless of how those threads are executed,
without requiring additional coordination from the calling code.
In other words, a thread-safe data type can be accessed and modified by multiple threads simultaneously without causing any issues or
requiring extra work from the programmer to ensure correct behavior.
2021-04-28 16:49:39 +02:00
2023-07-05 15:32:49 +02:00
Non-thread-safe objects are objects that are not designed to be used in a multi-threaded environment and can lead to race conditions and
data inconsistencies when accessed by multiple threads simultaneously.
Using them in a multi-threaded manner is highly likely to cause data problems or exceptions at runtime.
2021-04-28 16:49:39 +02:00
2023-07-05 15:32:49 +02:00
When a non-thread-safe object is marked as static in a multi-threaded environment, it can cause issues because the non-thread-safe object
will be shared across different instances of the containing class.
2021-04-28 18:08:03 +02:00
2023-07-05 15:32:49 +02:00
This rule raises an issue when any of the following instances and their subtypes are marked as `static`:
2021-04-28 16:49:39 +02:00
2023-07-05 15:32:49 +02:00
* `java.util.Calendar`,
* `java.text.DateFormat`,
* `javax.xml.xpath.XPath`, or
* `javax.xml.validation.SchemaFactory`.
== How to fix it
Remove the `static` keyword from non-thread-safe fields.
=== Code examples
==== Noncompliant code example
[source,java,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
public class MyClass {
private static Calendar calendar = Calendar.getInstance(); // Noncompliant
2023-07-05 15:32:49 +02:00
private static SimpleDateFormat format = new SimpleDateFormat("HH-mm-ss"); // Noncompliant
}
2021-04-28 16:49:39 +02:00
----
2023-07-05 15:32:49 +02:00
==== Compliant solution
[source,java,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
public class MyClass {
private Calendar calendar = Calendar.getInstance();
2023-07-05 15:32:49 +02:00
private SimpleDateFormat format = new SimpleDateFormat("HH-mm-ss");
}
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2023-07-05 15:32:49 +02:00
== Resources
2024-05-06 07:56:31 +01:00
2023-07-05 15:32:49 +02:00
=== Articles & blog posts
* https://web.mit.edu/6.005/www/fa14/classes/18-thread-safety/[MIT - Thread safety]
* https://www.baeldung.com/java-thread-safety[Baeldung - Thread safety]
* https://www.baeldung.com/java-static[Baeldung - Static]
2024-05-06 07:56:31 +01:00
=== Standards
* STIG Viewer - https://stigviewer.com/stig/application_security_and_development/2023-06-08/finding/V-222567[Application Security and Development: V-222567] - The application must not be vulnerable to race conditions.
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
2023-07-05 15:32:49 +02:00
2021-09-20 15:38:42 +02:00
== Implementation Specification
2023-07-05 15:32:49 +02:00
2021-09-20 15:38:42 +02:00
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Make "XXX" an instance variable.
2021-09-20 15:38:42 +02:00
endif::env-github,rspecator-view[]