7

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.

2
  • 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? Commented Jun 27, 2011 at 18:27
  • 1
    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 Commented Jun 27, 2011 at 18:58

4 Answers 4

19

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.

Sign up to request clarification or add additional context in comments.

8 Comments

+1. However, it should be emphasized that each class file will need to have the import added at the top of the file.
Hrm, another reason for me to not like java, but this does achieve the goal i was looking for in my code.
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
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!
@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?
|
4

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.

Comments

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....!!!

1 Comment

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
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 ) 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.