Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making methods available within classes?
#11
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.
Reply
#12
This was earlier discused
[Image: 2d75599e9a.png]:superman:
Reply
#13
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tutorial] Java Classes, Methods and Objects Eustaquio 0 992 10-14-2009, 03:55 AM
Last Post: Eustaquio

Forum Jump:


Users browsing this thread: 1 Guest(s)