4

I've been working on some problems from Project Euler, and, in the process, have written a lot of useful methods (in Java) that I might like to use in other Java projects. I want to be able to call them in the way that you call a function from java.lang.math, so if I had a method primeFactor() I could call it using MyMathMethods.primeFactor(number). How would I go about this? Would I make some kind of package that I could import? Would I make a superclass that includes all my useful math-y functions and have whatever class I'm working with in a new project extend that? There are probably multiple ways to do this, but I don't know what is best. Thanks in advance.

1
  • If it is in the same folder you wouldn't need to import it. If it is in a different folder, then you have to. Files are declared in a specific package at the top. For example: package java.lang; would be at the top of the Object class. So then, you can call import java.lang.Object to access the Object methods and constructors. Commented Jul 15, 2013 at 3:32

5 Answers 5

3

Mark your utility methods as public static. Package your classes containing those utility methods in a jar. Add/Refer that jar in your project, where you want to use the. Then in your code you can call them in a static way lke : MyUtilityClass.myUtilityMethod();

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

Comments

2

The best thing for this situation is to work in meaningful packages and make their jar
You can create a package like

/* File name : Animal.java */
package animals;

    interface Animal {
       public void eat();
       public void travel();
    }


Also on classes

package animals;

/* File name : MammalInt.java */
public class MammalInt implements Animal{

   public void eat(){
      System.out.println("Mammal eats");
   }

   public void travel(){
      System.out.println("Mammal travels");
   } 

   public int noOfLegs(){
      return 0;
   }

   public static void main(String args[]){
      MammalInt m = new MammalInt();
      m.eat();
      m.travel();
   }
} 

You can import them like
import animals.*; OR be more specific import animals.MammalInt;

Now you can make the jar file , import it in your project and use its method
You can eaisly do it by this command
jar cmf MyJar.jar Manifest.txt MyPackage/*.class
For more details about jar creation please see this
As a side note: Be carefull about visibility of members and functions while packaging it
Because there usage and accessibility matters a lot while we are using them

Comments

0

You could create separate java project with your util classes only and then create jar file and import into any another project.

Comments

0

Simply instantiate the class. Like your example, if you had a class MyMathMethods with the function primeFactor(number) then at other classes, simply instantiate it with something like private MyMathMethods myMathMethods;. Now, to call the function simply do myMathMethods.primeFactor(number); You may need to import its package as well.

Comments

0

False understanding of packages is any class defined within a package is visible to all other classes. Not true from my experience. If you have classes containing utility style methods you want to make available in another class? Simply declare a new instance of the class in the class you need the method in. Like... private MathUtilsClass mathUtilsClass = new MathUtilsClass(): Then any method you want to call from this class uses the new identifier, e.g. mathUtilsClass.greatFunction(); This is stupidly easy and should solve your problem.

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.