Saturday, 26 July 2014

clear : Clear the reference object

public void clear()
Clears this reference object. Calling this method will not cause this object to be enqueued in Reference Queue.

import java.lang.ref.WeakReference;

public class ClearReferent {
    static class Employee{
        String name;
        int id;

        Employee(int id, String name){
            this.id =id;
            this.name = name;
        }
        
        @Override
        public String toString(){
            return id +" " + name;
        }
    }

    public static void main(String args[]) throws InterruptedException{
        
        Employee emp1 = new Employee(1, "Krishna");
        
        WeakReference<Employee> weakRef;
        weakRef = new WeakReference<> (emp1);
        
        System.out.println(weakRef.get());
        System.out.println("Clearing the reference object");
        weakRef.clear();
        System.out.println(weakRef.get());
        
    }
}



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment