GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — Possible confusion of local and field — CodeQL query help documentation

Possible confusion of local and field¶

ID: java/local-shadows-field
Kind: problem
Security severity: 
Severity: recommendation
Precision: high
Tags:
   - quality
   - maintainability
   - readability
Query suites:
   - java-code-quality.qls
   - java-security-and-quality.qls

Click to see the query in the CodeQL repository

If a method declares a local variable with the same name as a field, then it is very easy to mix up the two when reading or modifying the program.

Recommendation¶

Consider using different names for the field and local variable to make the difference between them clear.

Example¶

The following example shows a local variable values that has the same name as a field.

public class Container
{
	private int[] values; // Field called 'values'
	
	public Container (int... values) {
		this.values = values;
	}

	public Container dup() {
		int length = values.length;
		int[] values = new int[length];  // Local variable called 'values'
		Container result = new Container(values);
		return result;
	}
}

References¶

  • Help - Eclipse Platform: Java Compiler Errors/Warnings Preferences.

  • Java Language Specification: 6.4 Shadowing and Obscuring.

  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.