Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Calling methods in other files

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calling methods in other files

    I am taking a beginning java class based in a unix environment. I have to create three .java files. One that prints out a simple line of text, one to call the method that prints the line of text and another file that contains the Main that calls the 2nd file. I have tried this a number of different ways to no avail so I am hoping someone can point me in the right direction.

    Thanks in advance and sorry if the above explanation is hard to understand.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Calling methods in other files

    Lesson: A Closer Look at the "Hello World!" Application (The Java™ Tutorials > Getting Started)

    Start with that. Write two very similar files with slightly different names. "HelloWorldApp.java" and "HelloWorldSecond.java" would be okay. Debug them both and run them both from the command line. They both have main methods. Make sure they print slightly different messages. Now change the System.out.println in the second to read something like:

     /* invoke main with a zero-length array */
    HelloWorldApp.main(new String[0]);

    Compile and run the second program again. Now you should see the first program's message. The second program has called the first program's "main(String[])" method. You can extend this to three Java files easily. The next step would be to make a nicer method than "main(String[])" to do the message printing. Get coding!

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calling methods in other files

    I tried that and It works but I the two other files that I am calling cannot have main methods. Can I use something like Public void init() somehow in place of the main?

  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calling methods in other files

    This is the code he gave us to follow as an example. Its for a random character generator but I can figure out how to use it simply for strings

    public class RandomCharacter {
    public static char getRandomCharacter(char ch1, char ch2) {
    return (char)(ch1 + Math.random()*(ch2-ch1+1));
    }}
    The class with the main
    public class TestLowerRandomCharacter{
    public class static void main(String args[]) {
    char ch = RandomCharacter.getRandomLowerCaseLetter();
    System.out.Println(ch);

    I cut some of the code out in the main class because im not going to use it anyway.

    Hope this will help you help me haha

    Thanks again

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calling methods in other files

    two other files that I am calling cannot have main methods.
    There are no restrictions on what name you use for methods in a java class. Any and all classes can have a method named: main.

    how to use it simply for strings
    What do you want the method to do?

Similar Threads

  1. [SOLVED] Calling methods from other classes
    By knightknight in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 7th, 2011, 06:16 PM
  2. Calling worker methods in JSF.
    By newbie in forum Web Frameworks
    Replies: 0
    Last Post: March 18th, 2011, 02:21 PM
  3. [SOLVED] Handling Errors / calling Error-Catching Methods
    By movsesinator in forum Object Oriented Programming
    Replies: 4
    Last Post: April 6th, 2010, 05:35 AM
  4. Calling exe files from Java
    By linuxrockers in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2010, 04:20 AM
  5. Calling for methods
    By soccer_kid_6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2010, 12:13 AM