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 6 of 6

Thread: Simple java method problem

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    18
    My Mood
    Nerdy
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Simple java method problem

    I have a problem with my program. I am trying to call a method from a secondary class in the main class.
    Whenever I debug the Test class I only get the text output "hello, Im the Test class!" or "hello Im the TestCall Class!" when I run the TestCall class.

    I want it to display either one of the two text outputs and the text "I am method showMessage from class Test!"

    This is the main class
    public class Test{
     
    public static void main(String args[])
    {
    System.out.println("hello, Im the Test class!");
    }
     
    public void showMesage(){
    System.out.println("I am method showMessage from class 
     
    Test!");
    }
    }

    And this is the secondary class:

    public class TestCall{
     
    public static void main(String args[])
    {
    System.out.println("hello Im the TestCall Class!");
    }
     
    public void run(){
    Test t = new Test(); // create an instance
    t.showMesage(); // use t to refer any method of class Test
    }
    }

    The first code I made had no:
    public static void main(String args[])
    {
    System.out.println("hello Im the TestCall Class!");
    }

    Then I received an error message when I tried to debug it: "Exception in thread "main" java.lang.NoSuchMethodError: main"


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Simple java method problem

    See Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects) To call a method, you must call the method. For example, in the second to last piece of code, call run() in the main method to run that piece of code.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    18
    My Mood
    Nerdy
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Simple java method problem

    I tried to do it this way but it still wont work.

    Main class:
    public class Test
    {
    public static void main(String args[])
    {
    System.out.println("hello Im the Test Class!");
    }
     
    public void run(){
    TestCall t = new TestCall(); // create an instance
    t.showMesage(); // use t to refer any method of class Test
    }
    }

    Secondary class:
    public class TestCall
    {
    public static void main(String args[])
    {
    System.out.println("hello, Im the TestCall class!");
    showMesage();
    }
     
    public static void showMesage()
    {
    System.out.println("I am method showMessage class 
     
    TestCall!");
    }
    }

    What I just did was compile the secondary class first, then i compiled the main class and ran the main class. It still wont call the method.
    I'm sure I'm doing a lot wrong here....
    And thanks for your reply

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Simple java method problem

    I'm not sure what you wish to run...both classes have a main method. Stick to a single main method and run the code from there.

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    18
    My Mood
    Nerdy
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Simple java method problem

    Okay... So I finally got it to work. I don't know though if this is the appropriate way of doing it.
    Here's my code, please tell me what you think.

    Main class:
    public class Test extends TestCall
    {
    public static void main(String args[])
    {
    Test nt=new Test();
    TestCall t = (TestCall)nt;
    t.showMesage();
    }
    }

    Secondary class:
    public class TestCall
    {
    public static void showMesage()
    {
    System.out.println("I am method showMessage class 
     
    TestCall!");
    }
    }

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Simple java method problem

    There is no reason for Test to extend TestCall.
    public class Test {
        public static void main(String args[]) {
            TestCall t = new TestCall();
            t.showMesage();
        }
    }

    Also, the showMessage method should not be static.

Similar Threads

  1. Very simple problem...PLEASE HELP!
    By dungeondragon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 1st, 2011, 07:19 AM
  2. Simple problem...
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 6th, 2011, 12:02 AM
  3. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  4. how to execute a simple display without a main method
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 13th, 2010, 07:28 AM
  5. [SOLVED] Java exception "result already defined"
    By rptech in forum Object Oriented Programming
    Replies: 3
    Last Post: May 20th, 2009, 05:48 AM