I'm extending a class, which has a lot of static
methods. But the client code extending my class will typically use just one or two of those methods. Is there a way that methods that are not used would not be compiled?
No, the compiler will not exclude any unused code snippet from making its way into the final compilation. The compiler can't infer that sort of information until runtime anyway; it doesn't know if this particular method will be used in one way or another.
However, you as a developer can most certainly prevent a scenario like this from happening.
There are tools out there, such as PMD, FindBugs, and Sonarqube which can run static analysis on your code to determine if there's any unused methods in your code. If you have a unit test suite, those tools can also help to show you uncovered/unhit code branches (such as an if
statement). A modern IDE like IntelliJ IDEA can also do the same while one is developing.
For your general situation though, there is some unnecessary inheritance going on; if you realistically only need to use 3 methods, elect to import them in as static imports instead of relying on inheritance to get the job done. This way, your code uses only the parts it needs to and doesn't have any of the excess baggage of what it doesn't need.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments