5

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 with any class that I can invoke from anywhere in my project (#include of some sort necessary?).

I know I can make a public static function of a class and invoke it as Class.someFunction();. I would like to just have someFunction(); I'm not sure if this is possible in java, or what the syntax for it is.

Dan F
  • 17,654
  • 5
  • 72
  • 110
  • What kind of utility class is this? Importing it when necessary surely can't be that bad; do you need it in absolutely every class? – Rob Hruska Jun 27 '11 at 18:27
  • It's not even a huge utility that I need, its a single function, that's why I didn't want to have to put it in a class – Dan F Jun 27 '11 at 18:58

4 Answers4

17

You can achieve the same "effect" by using a static import, by adding the following import in each file that you want to use it in:

import static x.y.Class.someFunction;  // or x.y.Class.*;

...

// some code somewhere in the same file
someFunction();

However, all methods in Java must be part of a class. Static imports just let you pretend (briefly) otherwise.

P.S. This also works for static fields.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • +1. However, it should be emphasized that each class file will need to have the `import` added at the top of the file. – StriplingWarrior Jun 27 '11 at 18:23
  • Hrm, another reason for me to not like java, but this does achieve the goal i was looking for in my code. – Dan F Jun 27 '11 at 18:23
  • The addition of the import is inevitable, even if I had some kind of puplic utility header file in any of the c-based languages, I'd still need the `#include` at the top, thats not that big of a deal to me – Dan F Jun 27 '11 at 18:24
  • Using IntelliJ IDEA, you can just type the method call you want to use and alt-enter will suggest a static import of that method. Easy as can be! – ColinD Jun 27 '11 at 18:41
  • @skaffman Just curious, do you know if this can work if `someFunction` has any access modifier itself, i.e. private, protected, public. For example importing a private method will no longer be private to that class it belongs to - thus it can be used by the class that imports it? – dbjohn Jan 21 '12 at 21:42
  • @dbjohn: Static imports do not change visibility, the same rules apply. – skaffman Jan 22 '12 at 10:54
  • @skaffman so even though a class may import a private method/field of another class, it could not use it because it is private to that other class? Correct? – dbjohn Jan 22 '12 at 11:52
  • @dbjohn: No, the compiler won't allow you to import it. You could just try this yourself, you know, and answer this. – skaffman Jan 22 '12 at 11:53
3

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.

Community
  • 1
  • 1
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
1

Also, following the programming best practices, You should define all such common, frequently used functionality in some utility class where you can define your functions or fields(probably constants- i.e. static and final attributes) that is going to be used/called at different places within the API.

Although, you still need to import the Utility class. Else define such functionality in the top most parent class in your API hierarchy structure, that way you even don't have to import the class.

Hope this helps. thanks....!!!

Barbareek
  • 52
  • 10
  • I was thinking about making a base class that has all those functions, it might obfuscate where the functionality exists if its a complex hierarchy, though – Dan F Jun 28 '11 at 13:10
0

Yeap import static..

For instance:

import static java.lang.Math.max; // Allowing to use max method anywhere in the source
class SomeClass { 
    int m = max( 1, 2 );// m now is 2 due t Math.max( int, int ) 
}
OscarRyz
  • 196,001
  • 113
  • 385
  • 569