Results 1 to 5 of 5
  1. #1
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Calling a Method

    I am having trouble trying to work out exactly how to call a method, I am trying to get "MainMenu.class" to call it from "NewGame.class" if that makes sense, would appreciate some help.

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

    Default Re: Calling a Method

    To call a method in another class, you need a reference to an instance of the other class, then code it like this:
    refToClass.theMethod(<any args here>);


    See also post#18 on this thread: If else problem
    Last edited by Norm; 09-24-2012 at 09:30 PM. Reason: clarify
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: Calling a Method

    When you said in a class do you mean inside the class that code is in or do you mean from another class?

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

    Default Re: Calling a Method

    There are two locations for code in this discussion. One is the location from which the call is being made and the other location is the code that is being called. I was talking about the case where the calling code was in one class and the called code was in another class.
    If the two locations are in the same class, then the reference to the current class would be: this which is the default and can be left off.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: Calling a Method

    That is what I am trying to do, call from another class, although I am thinking it would be easier to put it all in once class however I would rather keep it this was for learning purposes.

Similar Threads

  1. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 12:40 PM
  2. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 12:03 PM
  3. Calling a method
    By mnki23 in forum New To Java
    Replies: 21
    Last Post: 11-06-2009, 10:10 PM
  4. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 06:30 PM
  5. Replies: 29
    Last Post: 09-25-2008, 08:55 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
  •