Home » Java programming language

Java Observable clearChanged() Method with Example

Observable Class clearChanged() method: Here, we are going to learn about the clearChanged() method of Observable Class with its syntax and example.
Submitted by Preeti Jain, on March 04, 2020

Observable Class clearChanged() method

  • clearChanged() method is available in java.util package.
  • clearChanged() method represents that this object has no longer changed or in other words, we can say it has previously notified all of the observers of its most recent change.
  • clearChanged() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • clearChanged() method does not throw an exception at the time of requiring to changes the object.

Syntax:

    public void clearChanged();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void clearChanged() method of Observable

import java.util.*;

// Implement Observers class 
class Observers implements Observer {
    public void update(Observable obj, Object ob) {
        System.out.println("Obs is added");
    }
}

// Implement Observed Class
class Observed extends Observable {
    // Function call with setChanged()
    void setChange() {
        setChanged();
        System.out.println("setChanged(): " + hasChanged());
        notifyObservers();
    }

    // Function call without setChanged()
    void clearChange() {
        setChanged();
        System.out.println("clearChanged(): ");

        // By using clearChanged() method isto
        // unset all the changes done by setChanged()
        clearChanged();
        notifyObservers();
    }
}

public class ClearChanged {
    // Implement Main Method
    public static void main(String args[]) {
        Observed observed = new Observed();
        Observers obs = new Observers();
        observed.addObserver(obs);
        observed.setChange();
        observed.clearChange();
    }
}

Output

setChanged(): true
Obs is added
clearChanged(): 


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.