Solved

Java Observer

Posted on 1997-04-03
2
1,810 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Arrays.asList  VS  ArrayList 4 54
fibonacci ten numbers 4 38
MySqlDump not dumping triggers 1 30
Convert from a json string array to a Java object 3 28
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now