Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: method calling

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question method calling

    What would the code:

    stack.top().getX() do? I mean, what would it call?

    a.) call the getX() method of the result result by stack.top()
    b.) call the getX() method of stack, which doesn't exist

    When should I use:

    stack.top().getX()

    and when should I use :

    (stack.top()).getX()

    ?


  2. #2
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: method calling

    It will call the getX() of whatever top() returns

    The two examples at the end of your post would have identical results

  3. The Following User Says Thank You to DavidFongs For This Useful Post:

    javapenguin (November 3rd, 2010)

  4. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: method calling

    Quote Originally Posted by javapenguin View Post
    and when should I use :

    (stack.top()).getX()

    ?
    You only need parentheses if you need to cast the value returned by stack.top() to a Type that has the method getX().

    For example, in GUI coding an event has a getSource() method that returns Object. If you know what subclass of Object is the source of the event, you can cast it accordingly to use its methods.
    public void actionPerformed(ActionEvent e) {
      ((JButton) e.getSource()).setIcon(...);
    }
    // equivalent to
    actionPerformed(ActionEvent e) {
      Object o = e.getSource();
      JButton button = (JButton) o;
      button.setIcon(...);
    }
    db

  5. The Following User Says Thank You to Darryl.Burke For This Useful Post:

    javapenguin (November 4th, 2010)

Similar Threads

  1. Calling method from .class file
    By alexx_88 in forum Java Theory & Questions
    Replies: 6
    Last Post: April 24th, 2012, 02:14 AM
  2. Calling for methods
    By soccer_kid_6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2010, 12:13 AM
  3. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 10:24 AM
  4. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM
  5. Java Code Help - Calling Method
    By KevinGreen in forum Object Oriented Programming
    Replies: 5
    Last Post: September 18th, 2009, 12:55 AM