In java, I know I can use methods from other class by creating that class in my main myClass newClass = new myClass()
and then use methods by doing myClass.myMethod()
, where myMethod() is defined in myClass.java
, but what if I want a file named utils.java
that contains a bunch of useful methods, I just want to use a function that there's in this file, is this possible? How can I import and use the functions from that file?

- 100,966
- 191
- 140
- 197

- 43
- 1
- 8
-
Are the methods static? – khelwood Mar 17 '21 at 09:56
-
1I would suggest learning the basics of Java. – Tobias Brösamle Mar 17 '21 at 09:56
-
1You don't call methods from files in Java, you call methods on classes (static methods) or objects. Your `utils.java` or better `Utils.java` must contain a class, you can call its static methods. – Ondřej Xicht Světlík Mar 17 '21 at 09:57
-
@jhamon, yes that's what I want to do, but it's possible to import a function from file B, and use it directly with `myFunction()` without doing `B.myFunction()` ? – Silvano H. Mar 17 '21 at 10:20
-
@SilvanoH. [What is a good use case for static import of methods?](https://stackoverflow.com/questions/420791/what-is-a-good-use-case-for-static-import-of-methods) But would that really improve readability/maintainability of your code? – jhamon Mar 17 '21 at 10:30
-
it's actually just a curiosity to know how java works on these things, I'm learning it as a Node.js developer so I'm just discovering some similarities between these two languages, to improve my workflow with java :), thanks for your answers. – Silvano H. Mar 17 '21 at 10:38
3 Answers
You do not need to "import" the functions, you would need just to create a class
that keeps all your needed functions, and import that class.
I am supposing you use an IDE, you could go to your IDE and create a simple class
.
e.g:
public class Utils
{
public static int doSmth(/* Your parameters here */)
{
// Your code here
}
public static void yetAnotherFunction(/* Your parameters here */)
{
// Your code here
}
}
The most important keyword here is static
, to understand it I suggest you check my answer here: when to decide use static functions at java
Then you import this class to any of your other classes and call the static
functions, without creating an Object.
import package.Utils;
public class MainClass
{
public static void main(String[] args)
{
Utils.doSmth();
}
}

- 4,116
- 3
- 15
- 27
You can add at the beginning: import
like-
import utils;
Assuming it is in the same package as the current class
Else it should be:
import <package name>.<class name>

- 100,966
- 191
- 140
- 197

- 217
- 4
- 15
-
You cannot import things from a file called `utils.java` using `import utils`. – Mark Rotteveel Mar 17 '21 at 11:52
Yes, you can import methods but there are caveats. The methods are defined on a class. The methods are defined as static. The import employs the keyword static. Keep in mind that importing methods directly can create confusion and complicate debugging. Consider importing the class and invoke the method from the class. When/whether defining a utility class makes sense and how to implement them are separate discussions.
package some.path;
public class MyUtils {
public static int add(int x, int y) { return x + y; }
}
To import the method
package some.other.path;
import static some.path.MyUtils.add; // note keyword static here
public class MyClass {
private int a, b;
public int sum() { return add(a,b); }
}
Or, import the class and use the method statically
package some.other.path;
import some.path.MyUtils;
public class MyClass {
private int a,b;
public int sum() { return MyUtils.add(a,b); }
}

- 1,425
- 1
- 9
- 12