- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: java-best-practices/array-is-stored-directly
Language: Java
Severity: Notice
Category: Best Practices
In Java, it is recommended that constructors and methods clone any arrays received through parameters. This practice prevents the original array from being affected by any future changes made by the caller.
It is advisable to clone the array before storing it to ensure that you retain a copy of the array that will remain unaffected by any external changes made to the original array. By following this approach, you can promote code safety and maintain the integrity of the original array.
public class Foo {
private String[] x;
private int y;
public Foo(String[] param1, int param2) {
this.x = param1; // should make a copy of this array first
}
}
public class Foo {
private int[] x;
private int y;
public void foo (int[] param1, int param2) {
this.x = param1; // should make a copy of this array first
}
}
public class Foo {
private String[] x;
private int y;
public void foo (String[] param1, int param2) {
this.x = param1; // should make a copy of this array first
}
}
public class Foo {
private String[] x;
private int y;
public void foo (String[] param1, int param2) {
this.x = Arrays.copyOf(param1, param1.length);
}
}
public class Foo {
private String[] x;
private int y;
private void foo (String[] param1, int param2) {
this.x = param1; // no violation since the method is private
}
}
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products