Non-class functions in Java

前端 未结 4 1416
广开言路
广开言路 2020-12-21 16:46

I\'m mostly a c/C++/objective-C programmer, presently working in Java on an android application. My question is simple: I want a utility function, preferably not associated

4条回答
  •  萌比男神i
    2020-12-21 17:49

    You could use a static import:

    import static com.example.MyUtilityClass.*; // makes all class methods available
    // or
    import static com.example.MyUtilityClass.myMethod; // makes specified method available
    

    You don't see this used very often because, if overused, it causes harder-to-debug code (see the last paragraph at the link above).

    Here's a related question about when it's advisable to use this.

提交回复
热议问题