Support Forums

Full Version: Making methods available within classes?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Everybody is making this complicating.
I assume you just want to run a method that's in a different class than the one you are in currently.

In one class, you will have
Code:
public class MyApp {

    public static void main(String args[]) {
        // Print: Testing:
        Functions.print("Testing was: ");
        // Check Value
        if(Functions.check("test") ) { Functions.print("Successful!"); }
    }
}

Then, in the second class;
Code:
public class Functions {

    // Prints the given argument
    public static void print(String s) { System.out.println(s); }

    // Returns true if argument is "test"
    public static boolean check(String query) {
        if(query.equalsIgnoreCase("test") { return true; }
        return false;
    }
}

It's extremely simple stuff, so I havn't tested this code; I just typed it from the top of my head.
This was earlier discused
Something you might want to look into is a static class. I've never worked with one myself, but it would be a good way to group methods that you want readily available to all classes.

To give you an example - the math class is static.
Pages: 1 2