Definition

In computer programming, a weak reference is a reference that does not protect the referenced object from collection by a garbage collector, unlike a strong reference.

Uses

Examples

since Java 1.2 there are two types:-

for example

import java.lang.ref.WeakReference; public class ReferenceTest { public static void main(String[] args) throws InterruptedException { WeakReference wr = new WeakReference("I'm here"); StrongReference sr = new StrongReference("I'm here"); System.out.println("before gc: r=" + wr.get() + ", static=" + sr.get()); System.gc(); Thread.sleep(100); // only r.get() becomes null System.out.println("after gc: r=" + wr.get() + ", static=" + sr.get()); } }

eXTRA