Support Forums
Making methods available within classes? - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Java Programming (https://www.supportforums.net/forumdisplay.php?fid=22)
+---- Thread: Making methods available within classes? (/showthread.php?tid=478)

Pages: 1 2


RE: Making methods available within classes? - Virux - 10-12-2009

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.


RE: Making methods available within classes? - Akshay* - 10-12-2009

This was earlier discused


RE: Making methods available within classes? - wat - 10-22-2009

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.