All Downloads are FREE. Search and download functionalities are using the official Maven repository.

resources.report.rules.pmd.ConstantsInInterface.html Maven / Gradle / Ivy



ConstantsInInterface

ConstantsInInterface

Avoid constants in interfaces. Interfaces should define types, constants are implementation details better placed in classes or enums. See Effective Java, item 19.


//ClassOrInterfaceDeclaration[@Interface='true'][$ignoreIfHasMethods='false' or not(.//MethodDeclaration)]//FieldDeclaration
 

Example(s):


public interface ConstantInterface {
    public static final int CONST1 = 1; // violation, no fields allowed in interface!
    static final int CONST2 = 1; // violation, no fields allowed in interface!
    final int CONST3 = 1; // violation, no fields allowed in interface!
    int CONST4 = 1; // violation, no fields allowed in interface!
}

// with ignoreIfHasMethods = false
public interface AnotherConstantInterface {
    public static final int CONST1 = 1; // violation, no fields allowed in interface!
    
    int anyMethod();
}

// with ignoreIfHasMethods = true
public interface YetAnotherConstantInterface {
    public static final int CONST1 = 1; // no violation
    
    int anyMethod();
}
 




© 2015 - 2024 Weber Informatics LLC | Privacy Policy