Results 1 to 13 of 13
  1. #1
    Rockefella is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default How to call this method?

    Hi, first post, very new to Java. I would like to know how to call this method:

    Java Code:
    public static void name(hello [] myArray)
    I'd like to know how to call this method if it resides in the same method (main) as well as if if it resides in an external class.

    I tried something like: name(hello.myArray); but it doesn't work and I'm completely lost. Please, help!

    Thanks a lot in advance!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default

    You have to give it an array as an argument.

    I'd suggest going back and reading through the basic tutorials, namely this one: Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Rockefella View Post
    Hi, first post, very new to Java. I would like to know how to call this method:

    Java Code:
    public static void name(hello [] myArray)
    The method seems to take an array of type 'hello' as a parameter. I doubt if this is what you intended. Show us the complete method definition (you can leave out the body of the method).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Rockefella is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    The method seems to take an array of type 'hello' as a parameter. I doubt if this is what you intended. Show us the complete method definition (you can leave out the body of the method).

    kind regards,

    Jos
    Thanks a lot for your replies!

    The method looks as follows:

    Java Code:
    public static void sort(Disc [] myArray) {
    
            for (int i =0; i < myArray.length -1; i++ ) ... {
                
                }
    I would be very grateful for an answer as to how the method call have to look like. I've searched around but can't find anything (or maybe I'm too much of a noob to really see the info on the net.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default

    Quote Originally Posted by Rockefella View Post
    I would be very grateful for an answer as to how the method call have to look like. I've searched around but can't find anything (or maybe I'm too much of a noob to really see the info on the net.
    Did you even bother to click on the link I gave you? Did you read it?

    Again, the basic tutorials are you friend.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Rockefella is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Did you even bother to click on the link I gave you? Did you read it?

    Again, the basic tutorials are you friend.
    Yes, I actually did. Thanks. The thing is I have an intensive fever and don't feel like reading a lot today. I just need this to complete my second exercise and had hoped for a short answer to this so that I could go to sleep again before my head blows up.

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default

    Well here's a hint- how do you call any method?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Rockefella is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Well here's a hint- how do you call any method?
    referencevariable.methodname(argument); ?

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default

    Quote Originally Posted by Rockefella View Post
    referencevariable.methodname(argument); ?
    Looks good to me. What happened when you tried that?

    Is your question specifically about static methods? If so, the only difference is that you no longer need an instance of that class, you use the class directly.

    Hint- sort() is a static method of the Collections class that takes a List as an argument. To use it, I would do something like this:

    Java Code:
    Collections.sort(someList);
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Rockefella is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Looks good to me. What happened when you tried that?

    Is your question specifically about static methods? If so, the only difference is that you no longer need an instance of that class, you use the class directly.

    Hint- sort() is a static method of the Collections class that takes a List as an argument. To use it, I would do something like this:

    Java Code:
    Collections.sort(someList);
    A lot of errors. I've now tried putting this method in a separate class named CDSort and am trying to call it with:

    Java Code:
    CDSort cd = new CDSort();
                cd.sort(Disc [] cdArray);
    It says:

    unexpected type
    required: value
    found: class

    '.class' expected

    ';' expected

    Accessing static method sort

    I'm about to rip my brains out as we speak...

  11. #11
    Rockefella is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    And sort is a method I've written myself. Has nothing to do with collections.sort.

  12. #12
    Rockefella is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    Excuse me, myArray it is -- not cdArray. And it gives me errors.

  13. #13
    Rockefella is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    Finally got it right!

    Java Code:
    CDSort cd = new CDSort();
                cd.sort(myArray);
    Sorry to come off as an ignorant bastard. Peace to y'all!

Similar Threads

  1. call a JSP method
    By sauravsinha in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 09-26-2011, 04:19 PM
  2. Call Java Method in MS SQL
    By mexyz in forum New To Java
    Replies: 19
    Last Post: 02-17-2010, 01:38 PM
  3. how to call method?
    By leapinlizard in forum New To Java
    Replies: 9
    Last Post: 04-29-2009, 11:55 PM
  4. Call java method from jsp
    By saiphani723 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-25-2009, 11:21 AM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Posting Permissions

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