[Java] Final method can be static and public method can potentially be static

java

(Werner Thumann) #1

Description
This is an extension of the existing rule “squid:S2325” which reports private methods which can be static. It should be extended to report also final methods (with any visibility). Furthermore, methods which do not access any instance members but cannot be safely changed to being static because they may be overriden, should at least be marked as “can potentially be static”. The latter could either be an option in “squid:S2325” or a new rule so that the user can deactivate it independently from “squid:S2325”.

Type
Code Smell

Snippet

public class FinalMethodCanBeStaticAndPublicMethodCanPotentiallyBeStatic {

	/*
	 * squid:S2325 extension
	 */
	
	public final void m0() { // Noncompliant
		// here no access to non-static fields or methods in this class
		System.out.println();
	}
	
	public static final void m1() { // Compliant
		// here no access to non-static fields or methods in this class
		System.out.println();
	}
	
	/*
	 * Method can potentially be static
	 */
	
	public void m2() { // Noncompliant
		// here no access to non-static fields or methods in this class
		System.out.println();
	}
	
	public static void m3() { // Compliant
		// here no access to non-static fields or methods in this class
		System.out.println();
	}
	
}