Results 1 to 3 of 3
  1. #1
    theodorekon is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default How to use functions in separate files

    Hi, I'm writing a program in JAVA2 and since I want it to be as modular as possible I would like to create some functions (or routines) in separate files and then use them in the main program by calling them. For example lets say that in one file I have a function which takes 2 integers and returns their sum and I want to use it in my main program which is in another file.How can I do that??
    I know it must be trivial but no matter what I do when I compile the main program it says that it doesn't recognize the function so I'm forced to put the function inside the main program.

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    All Java code is in classes, and classes have methods -- basically the same thing as functions, but you may as well get used to Java terminology. Normal methods require an object -- an instance of the class in which they are defined. But there is a special kind of method called a static method, which does not require an instance, but rather a reference to the class as a whole.
    Java Code:
    public class Something {
            public int getValueFromASomethingObject() {
                    ...
                    return intValue;
            }
    
            public static String doSomeWork(int someParameter) {
                    ...
                    return stringValue;
            }
    }
    Java Code:
    public class MyProgram {
            public static void main(String[] args) {
                    Something myThing = new Something();
                    int myValue = myThing.getValueFromASomethingObject();
                    String myString = Something.doSomeWork(myValue);
            }
    }
    Hope that helps. Ask some more if it's not clear.

    -Gary-

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by theodorekon View Post
    Hi, I'm writing a program in JAVA2 and since I want it to be as modular as possible I would like to create some functions (or routines) in separate files and then use them in the main program by calling them. For example lets say that in one file I have a function which takes 2 integers and returns their sum and I want to use it in my main program which is in another file.How can I do that??

    classes in java should be implemented with high cohesion, that means with a specific focus. but for your purposes you can put all functions in one class or if you have a lot of functions you can put them in different classes. that's up to you but the main principle remains the same. that is to create a class for example named MyUtilities and declare the methods you want to access from outside with static, example

    Java Code:
    public class MyUtilities {
    
    	public static boolean saveStringToFile(String fileName, String saveString) {

    if you want to use the method saveStringToFile you can simple call MyUtility.saveStringToFile("c:/temp/test.txt", myStr). the static modifier allow to use that method without having an instance of that class. so add all your functions with public static and if you keep your class in the same package like the other classes all should work well.

Similar Threads

  1. How to separate return value ??
    By doha786 in forum New To Java
    Replies: 1
    Last Post: 03-10-2010, 03:43 PM
  2. jar files for oracle olap functions
    By sdas7 in forum Advanced Java
    Replies: 1
    Last Post: 10-12-2009, 07:00 PM
  3. How can my applet read from a separate .jar?
    By DenniGa in forum Java Applets
    Replies: 2
    Last Post: 03-27-2009, 03:34 PM
  4. Should I separate my code into separate files?
    By Inks in forum New To Java
    Replies: 0
    Last Post: 03-26-2009, 12:12 AM
  5. LOG4j Logging in Separate Files for Threads
    By krishna_85 in forum New To Java
    Replies: 0
    Last Post: 03-05-2009, 04:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •