Results 1 to 11 of 11

Thread: methods

  1. #1
    Zensai is offline Member
    Join Date
    Nov 2007
    Posts
    14
    Rep Power
    0

    Default methods

    MY PROBLEM: I dont know how to organize the main method.

    The program has 5 methods.

    The program has to start with the 1st method,
    then go to the 2nd (typing this at the end of the 1st method:
    Java Code:
    secondMethod(NewVector);
    Then the 3rd method has to start.
    The 3rd method uses an Array from the 2nd method.
    Finally the 4th method has to start.
    The 4th method needs an int calculation from the 5th method (the 5th method is 'private/public int' method with return).
    The 4th method 'goes' to the 5th through this:
    Java Code:
    if(nameOfFifthMethod==-1){...}
    The 5th method uses the Array list from the 2nd method and a String from the 4th method (that is different everytime the 5th method is being executed).

    So:
    Java Code:
    public static void main (String [] args) {[B]what should be written here?[/B] }
    only the 'request' to start the 1st method (and the others will be executed automatically)? all the method except the 5th method (the only one that returns)? all of the 5 methods? :confused:
    Last edited by Zensai; 11-30-2007 at 05:20 AM.

  2. #2
    sandeepkk2005 is offline Member
    Join Date
    Nov 2007
    Posts
    18
    Rep Power
    0

    Default

    In main, create an object of the class and call the 1st method. from there call 2nd, and in 2nd call 3rd etc.

  3. #3
    Zensai is offline Member
    Join Date
    Nov 2007
    Posts
    14
    Rep Power
    0

    Default

    why (=why we have to create an object) and how (we create an object, we call other methods)? thanx for your quick respond.
    Last edited by Zensai; 11-30-2007 at 06:11 AM.

  4. #4
    sandeepkk2005 is offline Member
    Join Date
    Nov 2007
    Posts
    18
    Rep Power
    0

    Default

    Object should be created to access any methods or variables inside the class.
    alternative is that u can use static methods & variables but is not preferred in Java.

    class Test
    {
    public Test()
    {
    }

    public void Method1()
    {
    //u can call method 2 or any other method here
    }

    public void Method2()
    {
    }

    public void Method3()
    {
    }

    public static void main(String args[])
    {
    Test test = new Test();//Creating an object to initialize
    test.Method1();//u can pass parameters here
    }
    }

  5. #5
    Zensai is offline Member
    Join Date
    Nov 2007
    Posts
    14
    Rep Power
    0

    Default

    Questions 1) why you created this empty method
    Java Code:
    public Test()
    {
    }
    and 2) why it has the same name with the class? (Lets say that the class name is 'ClassName' to make it easier.) 3) why the main method is at the end? 4)Can i do this (inside the main method): ClassName.method2(); 3 4 5 etc or i can do it only for one method? 5)how i call other methods when im inside a method? like this? (lets say we are inside the method 1):
    Java Code:
    nameOfMethod2.(and inside here name of the string, arrays etc method 2 uses from method 1);
    ? or can i do something like this
    Java Code:
    method1.method2([I]parameters taken from method 1 and used in method 2[/I]);
    (when i am inside the method1)? But for example, my 4th method doesnt 'need' anything from the 3rd method. (maybe im confused between two different procedures)

    can you please reply to them one by one? (it gonna be easier for me to understand them and faster for you to type them)
    1000 thanx a priori!


    PS java.lang.NoSuchMethodError: main
    Exception in thread "main" :(
    Last edited by Zensai; 11-30-2007 at 07:26 AM.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    You can add your main method anywhere inside the class. When you execute the program, it search the main method inside the class. There is no order to use them.

  7. #7
    Zensai is offline Member
    Join Date
    Nov 2007
    Posts
    14
    Rep Power
    0

    Default

    :confused:

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    When I go trough your questions, I feel that you are really newbie for Java. So it can be really helpful to you start learn those things first, with simple examples. I'll try to answer to you as much as possible.

    Quote Originally Posted by Zensai View Post
    why you created this empty method
    There is no point. If you want you can do anything inside a method.

  9. #9
    sandeepkk2005 is offline Member
    Join Date
    Nov 2007
    Posts
    18
    Rep Power
    0

    Default

    This empty method
    public Test()
    {
    }

    is called the constructor.
    I prefer you to go through Java basics.

  10. #10
    Zensai is offline Member
    Join Date
    Nov 2007
    Posts
    14
    Rep Power
    0

    Default

    ive read what a constructor is, how about the rest questions?

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    3) why the main method is at the end?
    There is no order to put the main method at the end. You can place it anywhere on the application. The normal practice is place the main method after all other functions/methods.

    When you run the application, it searches the main method first. No matter it is at the end or beginning.

Similar Threads

  1. JSP methods example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-30-2008, 11:00 AM
  2. Friendly methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-12-2007, 11:22 AM
  3. Methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 09:49 PM
  4. Using Deprecated Methods
    By ravian in forum New To Java
    Replies: 3
    Last Post: 11-23-2007, 08:58 PM
  5. Help with mathematical methods
    By Marcus in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 09:20 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
  •