Solved

Java Observer

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 60
hibernate insert example 13 34
null output 3 35
ejb entity bean example issue 2 16
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

861 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