Solved

Java Observer

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 39
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 41
Print Rhino Java Array in Javascript 1 32
Java array sort 10 30
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

740 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