Results 1 to 4 of 4
  1. #1
    antgaudi's Avatar
    antgaudi is offline Member
    Join Date
    Sep 2008
    Location
    Houston-TX
    Posts
    22
    Rep Power
    0

    Default [SOLVED] Call method from another class name

    With this code:
    public class First
    {
    public void myFirst()
    {
    }
    }


    The following is the CORRECT
    statement used if you want to call
    method myFirst() from another class
    named “Second”?
    a. First(myFirst);
    b. Second (myFirst);
    c. First.myFirst ( );
    d. Second.myFirst ( );
    e. The correct statement is not listed.

    The answer is "e"...
    What would be the actual correct statement? :confused:

    Thanks.
    "The minute you settle for less than you deserve, you get even less than you settled for." Maureen Dowd

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    To call a non-static method in another class, you need a reference to that class.
    First aRef = new First(); // create new instance and set reference
    aRef.myFirst(); // call the method

    If you already have a reference to that class, you don't need to create it. A reference can be passed in a method call:
    public void calledMethod(First aRef) {
    aRef.myFirst();

  3. #3
    antgaudi's Avatar
    antgaudi is offline Member
    Join Date
    Sep 2008
    Location
    Houston-TX
    Posts
    22
    Rep Power
    0

    Default

    what if I needed to call one that is static?
    "The minute you settle for less than you deserve, you get even less than you settled for." Maureen Dowd

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    than it would be c
    I die a little on the inside...
    Every time I get shot.

Similar Threads

  1. Need to dynamicaly get all parameters in a method call.
    By DavidJohns in forum Advanced Java
    Replies: 2
    Last Post: 05-21-2008, 07:15 AM
  2. Call Java Method
    By hussainzim in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-15-2008, 07:22 AM
  3. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM
  4. Call a Method Automatically
    By rhm54 in forum New To Java
    Replies: 4
    Last Post: 02-07-2008, 08:51 AM
  5. Unablt to call a sessionbean's business method in EJB 3.0
    By amitid4forum in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 11-24-2007, 12:00 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
  •