• Post Reply Bookmark Topic Watch Topic
  • New Topic

understanding order of how code is run

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a hard time (as code gets longer) understanding how it flows when it's run. For example, with the program below what
is the actual algorithm or tree that is being followed when someone clicks "suite"? Looking at it I would think it would stop at
the JMenuItem but it obviously doesn't. Is the rest of the programmed scanned and it picks up on the menu(suite) item?
I think line 77 is throwing me off because I'm confused how you can create an Object right there and it applies
to everything else.

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Bishop wrote:I think line 77 is throwing me off because I'm confused how you can create an Object right there and it applies
to everything else.

From the code you posted, line 77 is


Let's recap a bit on what the event delegation model is all about
1) Some actions trigger of events. e.g. Mouse move, mouse click, key typed, selection changed etc These events are automatically generated by the JVM
2) Typically you register a listener who will listen to such events
3) Depending on your requirement, you will process such events.

In your case, you achieved #2 by all those xxx.addActionListener(this); this being the SummervaleResort because it implements ActionListener
The actionPerformed() is the point where your listener gets notified about the events.
Since you have one single listener viz. SummervaleResort ready to listen to multiple events you need the ability to somehow figure out what triggered off the event. Imagine a dialog where you ask the user "Do you want to erase all your files?" with Yes and No buttons. Deleting files when the user clicked No (triggered the event) would be catastrophic.
Fortunately the designers of Java though of this problem and the JVM encapsulates the event source (which triggered off the event) inside the ActionEvent (the notification your listener receives), which you extract using the convenient getSource() method.

Now everything in Java is an object (except primitives like int of course) So the return type of the getSource is of Object type.
how you can create an Object right there

As you can see, you are not creating the object, but just referencing the one which was encapsulated inside the ActionEvent for you by the JVM
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all becomes more complicated in Java8 where you can pass code instead of an object. I think you would have to say, it is the Component which has the appropriate listener which catches the Event.

There is a section in the Java Tutorials, but I suspect it will not tell you anything that Maneesh hasn't already told you better.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. Do either of you recommend a book or website that focuses on java syntax?
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Language Specification
Java Virtual Machine Specification

Both difficult to understand.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!