Solved

Java Observer

Posted on 1997-04-03
2
1,813 Views
Last Modified: 2008-03-04
I am having difficulties using Java Observer and Observable.

This is how I tried, but it didn't work. Please email
me with the mistakes I am making.

class x_class extends Observable
{

    LinkedList TL;
--
--
--
   notifyObserver(TL)
--
--
--
}

class y_class extends Object implements Observer
{
--
--
--
   void update(x_class x, LinkedList L)
{
--
--
--
}

in the init function in the applet

void init()
{
    y_class y = new y_class;
    x_class x = new x_class;
    x.addObserver(y);
}

I redd the answer urgently.


Thank you,
0
Comment
Question by:rparamal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Accepted Solution

by:
koehn earned 100 total points
ID: 1219405
From the source code (Observable.java):
    /**
     * Notifies all observers of the specified observable change
     * which occurred.
     * @param arg what is being notified
     */
    public synchronized void notifyObservers(Object arg) {
      if (hasChanged()) {
          if (obs != null) {
            if (obs instanceof ObserverList) {
                ((ObserverList) obs).notifyObservers(this, arg);
            } else {
                ((Observer) obs).update(this, arg);
            }
          }
          clearChanged();
      }
    }

So you have two options:
- override hasChanged() to always return true

- call setChanged() before calling notifyObservers(arg) {recommended!}

0
 
LVL 1

Expert Comment

by:gerry99
ID: 1219406
The answer given is correct.  'setChanged' must be called before 'notifyObserver'.  Note: 'notifyObserver' will automatically clear the changed state of the Observable class.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
login form jsp example 2 71
ejb message driven bean mdb creation steps 2 54
running on tomcat not jboss eap 7.0 3 53
Strange router problem - can't access hotmail.com 14 108
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question