[Java] Indirect access to static member

java

(Werner Thumann) #1

Description
Static members should not be accessed via derived classes. It is confusing and may lead to different behavior if another static member with the same signature is added to the derived class.

Type
Code Smell

Snippet

public class IndirectAccessToStaticMember {

	public static class Base {
		public static String method() {
			return "hello world";
		}
	}
	
	public static class Derived extends Base {
		
	}
	
	public static void main(String[] args) {
		System.out.println(Derived.method()); // Noncompliant
		System.out.println(Base.method()); // Compliant
	}
	
}

Note
Also covered by Eclipse JDT.