user2582102
user2582102

Reputation: 61

How to use java methods defined in another class/file

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.

Upvotes: 4

Views: 52151

Answers (5)

Lester
Lester

Reputation: 1

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.

Upvotes: 0

Freak
Freak

Reputation: 6883

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

Upvotes: 2

Mark M
Mark M

Reputation: 1600

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.

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

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();

Upvotes: 3

Alex
Alex

Reputation: 11579

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

Upvotes: 0

Related Questions