Jump to content
skaterdav85

What are Observers in Java

Recommended Posts

Can someone explain to me what are observers used for in Java? I'm confused on the whole idea of using listeners and observers in Java since most of my experience of application interactivity is on the web with JavaScript.

Share this post


Link to post
Share on other sites

I was trying to understand the example but i dont see what the difference between an observer and a listener is. Maybe you can compare it to JS even though I know these terms don't apply. In JS, it seems like a 'listener' would be adding an event handler to some html object, so these would be comparable:

JSonClick="someFunction();"JavaaddWindowListener(new LocalWindowListener());

What would the 'observer' be?

Share this post


Link to post
Share on other sites

JS's object-oriented model isn't that great, so there is no real comparison. Basically, an observer watches another object for something to happen (doesn't have to be user-driven), and then gets notified when that thing happens. They are a bit like "listeners" in JS - but can you think of what that would be in Java?

Share this post


Link to post
Share on other sites

So would an observer be added to some instance variable or object and then the update method of the observer class would be called when say the value of the instance variable or the object changes?

Share this post


Link to post
Share on other sites

Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.http://www.globalguideline.com/interview_q...ava_Programming

Edited by Chathu

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...