Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

how to use observable class using collection in java

how to use observable class using collection in java

Previous Home Next

 

In this example we shall see how to use Observable class in collections.

For using the Observable class we have to import the java.util package in which the java.util.Observable class is defined. In this example we have created 3 classes.
First class we have created is the Employee class which extends the Observable class.
Second class we have created is the Managers class which implements the Observer interface.
Third and final class is the CollectionsExample class which contains the main() method.
 In the class Employee we have created method named setSalary(double new_emp_sal) which takes the double value as a parameter. Inside this method we have used the following methods which are as follows,
boolean hasChanged()
This method checks if the current object has changed or not. It returns true if the object has changed else it will return false.

setChanged()
This method marks the current Observable object as  changed.

void notifyObservers(Object obj)
The method notifies that if the specified object has changed which is indicated by the hasChanged method. This method will notify  all of its observers and then will call the clearChanged() method  to indicate that the current object has no longer changed.

protected void clearChanged()
The above method suggests that the current object has no longer changed,  so now  the hasChanged method will now return false.

In this example we have a class named Employee which extends the Observable class, in which we have created a method named setSalary(double new_emp_sal) which takes care if there is any change in the salary of an employee.
For checking the same we have used the hasChanged(), setChanged(), notifyObservers(Object obj) and clearChanged() methods on the attribute salary of the Employee class.

We have also created another class named Managers which implements the Observer Interface. Inside this class we have implemented the update(Observable obj, Object arg) method of the Observer interface.

Lastly, we have created a class named CollectionsExample in which we have defined the main() method. Inside this class we have created an object of the Employee class and invoked setSalary(double new_emp_sal) on it, which changes the current salary of the employee.
After that, we have created an object of the Managers class i.e m and invoked the update(Observable obj, Observable arg) on it. Then we have called the addObserver(Observer obj) on the object of the Employee i.e. empployee by passing the object of Managers class, which is m, as an argument.
Finally we have showed the number of observers by using the countObservers() method the employee.
The countObservers() method returns an integer value and returns the number of observers of the current Observable object.

 
package r4r.co.in;
import java.util.*;
import java.util.Observable;

public class CollectionsExample {

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
try
{
Employees employee = new Employees(); // creating the object of Employee class
System.out.println("The previous salary of employee is: "+employee.emp_sal); // retrieving the initial salary of employee
employee.setSalary(14500.7654); // setting the new salary
Managers m = new Managers(); // creating the object of the Manager class
m.update(employee, employee.emp_sal); // calling the update method defined in Manager class
employee.addObserver(m); // adding the obsever m to the employee
System.out.println("The number of observers are: "+employee.countObservers()); // counting the number of observers defined
employee.deleteObserver(m); // deleting the observer m
System.out.println("After deleting the observer, now the number of obsevers is: "+employee.countObservers()); // again counting the number of observers


}

catch(NullPointerException e)
{
e.printStackTrace();
}
}

}

class Employees extends Observable // creates a class Employee which extends the Observable class
{
double emp_sal=10000.09; // defining a double type variable named emp_sal

/* The following method set the new salary of the employee and registers the various changes*/

void setSalary(double new_emp_sal)
{
System.out.println("Initially there is any change???:--> "+hasChanged()); // checks whether initially there is any change in the salary
emp_sal=new_emp_sal; // here we have change the salary of the employee
setChanged(); // we have mark that the object(emp_sal) has changed,
System.out.println("After changing the salary, there is any change ???:--->"+hasChanged()); // checking whether the salary has been changed or not
notifyObservers(this); // this notify all observers of the change been made
clearChanged(); // here we have clear the change and define no further change has been made
System.out.println("After using clearChanged() method, is there any change???:--->"+hasChanged()); // check whether any changes have been made
}

}

class Managers implements Observer // here we are creating a class Manager which implements the Obsever interface
{

public void update(Observable obj, Object arg) // here we are implementing the update method of Observer interface
{
System.out.println("The new(changed) salary of the employee is: " + arg); // displaying the changed object
}

}


The output of the above given example is as follows:

The previous salary of employee is: 10000.09
Initially there is any change???:--> false
After changing the salary, there is any change ???:--->true
After using clearChanged() method, is there any change???:--->false
The new(changed) salary of the employee is:  14500.7654
The number of observers are: 1
After deleting the observer, now the number of obsevers is: 0

Previous Home Next