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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.