When extending class with static methods that I don't actually use, will those methods be compiled?

Pape

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?

Makoto

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

When to use static and class methods

From Dev

Should I Leave Methods I Don't Use In a Class?

From Dev

Extending a class which has static methods

From Dev

When should I use static methods?

From Dev

When my app is closed Strings are returning null in a class extending BroadcastReceiver when references static methods in another class

From Dev

Android - Is it OK to use a custom class for static members and methods instead of extending an Application class

From Dev

Are there compiler optimisations for methods of a templated class that don't use the template parameter?

From Dev

I don't know if I need to or how to implement those 3 basic methods of an interface

From Java

Java: when to use static methods

From Dev

What is the reason you can't use static methods/variables in a class

From Dev

Is there a workaround to use static methods by a generic class?

From Dev

Instantiate a class to call a method or use static methods?

From Dev

Is there a workaround to use static methods by a generic class?

From Dev

Why must I use keyword static when they don't have to?

From Dev

Static Methods ok to use when using parameters?

From Dev

Get the static methods of a class

From Dev

Implementing Static Methods In A Class

From Dev

MakeGenericType() actually makes an object, and I cannot use the type's methods

From Dev

Static class variables are created when Static methods are being called - Java

From Dev

Is it possible to extend a class without extending the methods of this class?

From Dev

Use 'MutableClass' methods in 'Class' methods

From Dev

Java: Use Static methods of Parent Class in Child Class

From Dev

Java: Use Static methods of Parent Class in Child Class

From Dev

How can I use the same generic type in multiple methods in a non-generic static class

From Dev

PHP: When to use Traits and when to use static methods?

From Dev

Class methods - which one to use and when?

From Java

Testing abstract php-class static methods that need extending classes to be meaningful

From Java

How do I get a warning in Visual Studio when async methods don't end in 'Async'?

From Dev

How to mock Python static methods and class methods

Related Related

  1. 1

    When to use static and class methods

  2. 2

    Should I Leave Methods I Don't Use In a Class?

  3. 3

    Extending a class which has static methods

  4. 4

    When should I use static methods?

  5. 5

    When my app is closed Strings are returning null in a class extending BroadcastReceiver when references static methods in another class

  6. 6

    Android - Is it OK to use a custom class for static members and methods instead of extending an Application class

  7. 7

    Are there compiler optimisations for methods of a templated class that don't use the template parameter?

  8. 8

    I don't know if I need to or how to implement those 3 basic methods of an interface

  9. 9

    Java: when to use static methods

  10. 10

    What is the reason you can't use static methods/variables in a class

  11. 11

    Is there a workaround to use static methods by a generic class?

  12. 12

    Instantiate a class to call a method or use static methods?

  13. 13

    Is there a workaround to use static methods by a generic class?

  14. 14

    Why must I use keyword static when they don't have to?

  15. 15

    Static Methods ok to use when using parameters?

  16. 16

    Get the static methods of a class

  17. 17

    Implementing Static Methods In A Class

  18. 18

    MakeGenericType() actually makes an object, and I cannot use the type's methods

  19. 19

    Static class variables are created when Static methods are being called - Java

  20. 20

    Is it possible to extend a class without extending the methods of this class?

  21. 21

    Use 'MutableClass' methods in 'Class' methods

  22. 22

    Java: Use Static methods of Parent Class in Child Class

  23. 23

    Java: Use Static methods of Parent Class in Child Class

  24. 24

    How can I use the same generic type in multiple methods in a non-generic static class

  25. 25

    PHP: When to use Traits and when to use static methods?

  26. 26

    Class methods - which one to use and when?

  27. 27

    Testing abstract php-class static methods that need extending classes to be meaningful

  28. 28

    How do I get a warning in Visual Studio when async methods don't end in 'Async'?

  29. 29

    How to mock Python static methods and class methods

HotTag

Archive