Results 1 to 3 of 3
  1. #1
    Eric is offline Senior Member
    Join Date
    Jun 2007
    Rep Power

    Default I need didactic explanation

    Need a detailed explanation on how this works, I know it does, but not sure how to explain it to some one else.

    Java Code:
    public void actionPerformed(ActionEvent event) {
    int value = 0;
    if (event.getSource() == tryAgain)
    else if (event.getSource() == one)
    value = 1;
    else if (event.getSource() == two)
    value = 2;
    else if (event.getSource() == three)
    value = 3;
    if(value > 0) {
    guess *= 10;
    guess += value;


  2. #2
    Albert is offline Senior Member
    Join Date
    Jun 2007
    Rep Power


    Somewhere up in the code there are a few objects (hard to tell what they are from this code) that are named tryAgain, one, two, and three. Something is triggering an event on these that is calling actionPerformed.

    I would "guess" that guess is defined as an int somewhere else. It looks like if the event came from tryAgain then you reset the guess. Otherwise you do the math as shown.

    Does that make sense?


  3. #3
    Marcus is offline Member
    Join Date
    Jun 2007
    Rep Power


    Looking at that code, I would assume that the four objects, tryAgain, one, two, and three, all share the same action listener, and this method is the actionPerformed(ActionEvent) method within that class. When an event is triggered, this method is called.

    Then, you check four things. First, you check if the object that triggered the event (event.getSource()) is the same object as tryAgain. You perform the same check three more times with different objects.

    Note that == compares memory. The code works because when objects are passed into methods the value is not passed in, but a reference of the memory location.



Similar Threads

  1. Class explanation
    By mcal in forum New To Java
    Replies: 1
    Last Post: 02-05-2008, 06:50 PM
  2. need a little explanation
    By cew27 in forum New To Java
    Replies: 7
    Last Post: 12-13-2007, 11:39 PM
  3. Explanation bout threading and concurrency?
    By cruxblack in forum New To Java
    Replies: 1
    Last Post: 08-10-2007, 10:33 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