• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can someone explain this bit of code please?  RSS feed

 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This comes from the book "Java After Hours", cool book by the way. I can follow most of the code, and I am playing with it to match some design patterns for practice and make it a bit more interesting. This part I can't follow or find a decent explaination.



Also is this good practice?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the constructor for this class, we're defining an anonymous inner class, a class without a name that extends WindowAdapter. You can only define such a class by creating an instance of it, so that's being done here. That instance is being passed to addWindowListener() on the Frame, and in the appropriate event-listener method, it's calling System.exit(). The end result is that when the Frame is closed, the JVM will exit.

For JFrames, you can use theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); but java.awt.Frame doesn't offer this facility.

So is it the anonymous inner class that's new, or the WindowAdapter, or...?
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The anonymous inner class is what was confusing me. I more or less understand the awt part of it. That makes sense and I can get more info on it, now that I know basically what is going on. Thank you.

So is this a desirable thing to do, or is a more traditional approach better? Like implement the appropriate listener interfaces in Aquarium?
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've always thought the syntax for an anonymous inner class seemed confusing, especially when you're trying to look at someone elses undocumented code.

It is pretty handy when you only need to implement one of the interfaces methods, however.

Drew
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you. I read up on it a bit and I agree. I see its value now though. I am going to implement a pop-up window to specify how many fish or can add sharks(they will occasionally eat a fish) or even a whale, change backgrounds ect. The event to trigger the pop-up window will be a right click, and having to add all the MouseListener methods is pointless here.

I think it will help me learn threading better, plus it just sounds fun. Oh yeah, adding dolphins that jump will be cool to.

I think the hard part will be changing the code that controls motion. It is a little herky-jerky the way it is implemented in the book and I am trying to develop smoother motion. But that just might be a post for another day(probably month since I am still in school).
[ May 21, 2006: Message edited by: Rusty Shackleford ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!