Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Method call help!!!

Options
  • 23-01-2004 1:49pm
    #1
    Closed Accounts Posts: 1,637 ✭✭✭


    Hi,

    I want to call a function without passing anything down to it,

    Also is it possible to call a line of code e.g. (Call 22) so it would run the code from that line.
    __________________________________________________

    import javax.swing.JOptionPane;

    public class method {


    public static void main(String[] args) {


    result = two(); //Method Call two

    System.out.print(result);

    }
    public static int two(int x){ //Method Call two

    return "hello";

    }

    }
    __________________________________________________


    Thanks loads in advance....

    Thanks

    joePC


Comments

  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Ahh I see.

    basically your main is in your class, but the main itself is not your class.

    You have to create an object of your class before you can use the method.

    eg...

    method myMethod = new method;

    result = myMethod.two(); //Method Call two (note to self, proofread)

    Heh.. you can tell I have not had much sleep :)

    Also you have your two() method without an argument. Which means you have to create a method without an argument for this to work or supply an int for the method you already have written.


  • Closed Accounts Posts: 1,637 ✭✭✭joePC


    Also you have your two() method without an argument. Which means you have to create a method without an argument for this to work

    How do I go about this?

    Thanks for the reply,

    joePC


  • Registered Users Posts: 4,185 ✭✭✭deadl0ck


    Actually you don't need to have an instance of the class as the method you're trying to call is static.

    All you need in main is:

    method.two();

    Also you're returning a string from the two method, not an int....and you're not using anythin from the JOptionPane class so you don't need the import either:

    [PHP]
    public class method
    {
    public static void main(String[] args)
    {
    String result = method.two(); //Method Call two
    System.out.print(result);
    }

    public static String two() //Method Call two
    {
    return "hello";
    }
    }
    [/PHP]


  • Registered Users Posts: 3,886 ✭✭✭cgarvey


    Originally posted by joePC
    How do I go about this?

    Thanks for the reply,

    joePC


    public static int two( int x ) {
    return "hello";
    }

    public static int two() {
    return "hello";
    // or it could be...
    // return this.two( 0 );
    }

    HTH
    .cg


  • Registered Users Posts: 4,185 ✭✭✭deadl0ck


    That won't work - as you're returning a string not and integer


  • Advertisement
  • Closed Accounts Posts: 1,637 ✭✭✭joePC


    Thanks loads for the help everyone.....

    deadl0ck --> thats exactly what I was looking for.

    Thanks joePC


  • Registered Users Posts: 3,886 ✭✭✭cgarvey


    Ooopss, you're right, I just copied from original post .. so, it should be ..


    public static String two( int x ) {
    return "hello";
    }

    public static String two() {
    return "hello";
    // or it could be...
    // return this.two( 0 );
    }

    .cg


    (Edit: but I see you've already posted the example above anyway.. Jeepers what the hell was I doing on the compose page for 6 mins :) )


Advertisement