Matthew G
Matthew G

Reputation: 1090

Include class full of reusable functions in another class

In PHP, I can have a file like so:

function isPrime($num)
{
    return true;
}

function sayYes()
{
    echo "yes\n";
}

This will be in a file named functions.php in a folder named mycode.
I have another file:

include "../functions.php";
if (isPrime()) {
    sayYes();
}

This is in a file named file1.php in a folder named file1. The file1 folder is inside the mycode folder.

The point of this example is that I have a file with a whole bunch of functions that I want to be able to re-use in other files. I'm going to have many folders inside the mycode folder (file1, file2, file3 etc). All of the code inside each of these subfolders is completely separate and unrelated to all of the code in all of the other subfolders. However, there are some generic functions I want to use across code in all subfolders, and as such it is placed in the top-level folder.

In Java, I have a file like so:

package com.awesome.mycode.file1;

public class File1
{
    public static void main(String[] args)
    {
        MyCodeFunctions.sayYes();
    }
}

This is in a file named File1.java in the same file1 folder as above.
I have another file like so:

package com.awesome.mycode;

public class MyCodeFunctions
{
    public static void sayYes()
    {
        System.out.println("yes");
    }
}

This is in a file named MyCodeFunctions.java, in the same mycode folder as above.

My question is this:
How do I compile these two classes such that I can reuse MyCodeFunctions.java in classes in many different sub-folders underneath the folder where MyCodeFunctions.java is located? What sort of import statement do I need to place at the top of File1.java? Do I need to compile MyCodeFunctions.java into a JAR file? If so, how? What do I run on the command line to link this JAR file in when compiling File1.java?

I understand that this is a different paradigm to the kind of include statements you find in languages such as PHP. That is the whole point of this question. I understand there is a difference, and I want to know what the equivalent way of doing it in the Java paradigm is. I do not want to use an IDE to do this, and it does not matter to me how complicated it is doing it manually.

I'm using Linux to do this, but I can just as easily do it on a Windows machine.

Upvotes: 0

Views: 912

Answers (1)

Ted Hopp
Ted Hopp

Reputation: 234857

In Java, you would use the import statement:

package com.awesome.mycode.file1;

import com.awesome.mycode.MyCodeFunctions;

public class File1
{
    public static void main(String[] args)
    {
        MyCodeFunctions.sayYes();
    }
}

The import is unnecessary for classes in the same package.

Since sayYes() is a static function, there's a variation that would allow you to use the function without the class qualifier:

package com.awesome.mycode.file1;

import static com.awesome.mycode.MyCodeFunctions.sayYes;

public class File1
{
    public static void main(String[] args)
    {
        sayYes();
    }
}

You can use a * as a wildcard in the last position to import all classes (for the import statement) or all static members—both fields and methods&mdash (for the import static statement).

EDIT

To compile both files, your source tree should look like this:

/com
    /awesome
        /mycode
            /file1
                File1.java
            MyCodeFunctions.java

To compile, cd to the folder containing /com and use the command line:

javac com/awesome/mycode/MyCodeFunctions.java com/awesome/mycode/file1/File1.java

along with whatever other options you want (to control output, etc.) Consult the javac docs for more info.

Upvotes: 2

Related Questions