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

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();
	}
	
}

Hi,

Indeed it looks like we could extend this rule to report on final methods and classes (ticket created).

Still I don’t think we should apply your suggestion about public methods. We try not to raise “recommendation” issues, but only those which can actually be fixed.

1 Like

Hi Elena,

I’ve noticed that the ticket was resolved (actually quite some time ago). However, the rule title still mentions only private methods which can be made static. Could you create a ticket to change the title and also the rule description?
I’d say something along Non-overridable methods that don’t access instance data should be “static” for the title and something similar for the description would be fine. Right now it looks like the rule is buggy.

Hello @oliver,

Make sense to me, thanks for spotting this inconsistency.
I updated the rule description, it will be available for the next release.

Best,
Quentin

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.