Separate lines for each field declaration


ID: java-best-practices/one-declaration-per-line

Language: Java

Severity: Notice

Category: Best Practices


In Java, it is possible to declare multiple variables of the same type on a single line using commas. This can make code more cluttered and less readable. Declare each variable on a separate line to improve code clarity and maintainability.

Non-Compliant Code Examples

public class Person {
    // combined declarations (same line) - a violation
    String firstName, lastName;

    // combined declaration (diff line) - no violation
    String firstName,

Compliant Code Examples

public class Person {
    // separate declarations - no violation
    String firstName;
    String lastName;     
} jetbrains

Seamless integrations. Try Datadog Code Analysis