Repository navigation
Description
Proposed Rule Name:
LocalVariableDeclarationsShouldBeAtStartOfBlock
Alternative name: InterleavedLocalVariableDeclarations
Proposed Category:
Code Style
Description:
Reports an ordinary local-variable declaration statement that appears after another kind of block statement in the same brace-delimited block.
For this rule, a declaration section is the maximal leading sequence of local-variable declaration statements in a block. Comments and blank lines do not end the declaration section. Each nested block is analyzed independently.
Some projects use a declaration section so that local variable names and explicit types can be found in one predictable location at the beginning of
each scope. This rule would allow those projects to enforce that coding standard consistently.
My team puts local variable declarations without initialization at the top of the brace-delimited block (e.g., method, loop). They then initialize the variables later. This simplifies each line of code. In our opinion, it makes the code easier to read than declaring and initializing together. We find it helpful to be able to quickly find the variable declaration at the top of the block rather than having to hunt through the code.
This rule represents an alternative to VariableDeclarationUsageDistance. It is a separate rule proposal and is not a request to change that rule or the behavior discussed in #6846. Projects generally would not enable both placement policies at the same time.
Note: The rule needs to allow for this(...) and super(...) to be at the top of the constructor and then followed by local variables. This allows for compatibility with Java 24-. JEP 512 Flexible Constructor Bodies allows for local variable declarations to be placed before this(...) and super(...). This was released with Java 25.
Note: The compiler requires that var local variables be initialized at the declaration site. This rule must ignore variables declared with var.
Code Sample:
There are two places where the rule should flag this code. First, encoded is declared at the top but it is also assigned. When there are a lot of local variables, the assignments make it hard to read the declarations. Second, decoded is declared in the middle. If the method were longer and I was near the bottom, it would take me a while to figure out what the type of decoded is because I have to read through the method instead of quickly looking at the top of the block.
private String getPassword()
{
byte[] encoded = GlobalConfigMap.
getInstance().
getVault().
secret("database.password").
getValue();
byte[] decoded = Base64.
getDecoder().
decode(encoded);
return new String(decoded, StandardCharsets.UTF_8);
}The rule should not flag anything in this code. All of the local variables are declared at the top of the block. If the method were longer and I was near the bottom, I can quickly look at the top and find what the type of decoded is. Spending less time hunting improves readability and code comprehension.
private String getPassword()
{
byte[] decoded;
byte[] encoded;
encoded = GlobalConfigMap.
getInstance().
getVault().
secret("database.password").
getValue();
decoded = Base64.
getDecoder().
decode(encoded);
return new String(decoded, StandardCharsets.UTF_8);
}Possible Properties:
sortBy: [name | type | none (default)] - enforces that the variables must be sorted alphabetically by variable name, variable type, or sorting is not required (i.e., none). The default is none since the rule name says nothing about sorting. The sort only applies to the first variable if multiple are declared on the same line such as int b, a;. (Note: SonarQube discourages declaring multiple variables on a single line.)
caseSensitive: [true | false (default)] - if sortBy is set, then the sort defaults to case insensitive sorting. For example, case insensitive sorting allows the local variable timed to come before timeElapsed which in my opinion makes it easier to find.
requireBeforeThisSuper: [true (default) | false] - true requires local variable declarations to be before this(...) and super(...). Projects on Java 24- will need to configure this to false.
Activity
victorwss commentedon Jul 15, 2026
I really dislike this. Please, let this be off by default.
UncleOwen commentedon Jul 15, 2026
@victorwss PMD imposes no default. It's up to the user to select which rules to use and which not to use. That is especially true for the code style category.
Will-6543 commentedon Aug 1, 2026
Working on this now.
Update release notes (pmd#6882, pmd#6923)
[-][java] New Rule: Enforce Local Variable Declarations to Start of the Block[/-][+][java] New Rule: Enforce local variable declarations to be at the start of a block[/+][java] Fix pmd#6882: New Rule: Enforce that local variables are decla…