Say I have a class that wants to provide instances of itself via a static method. The instances require the use of a `Context`, so the method will be called like this:

```
Foo foo = Foo.getInstance(context);

```

I'm thinking of doing it like this:

```
public class Foo {
    private static final Map<Context, Foo> instances = new WeakHashMap<>();
    private final WeakReference<Context> weakContext;

    private Foo(Context context) {
        if(context == null) throw new NullPointerException();
        weakContext = new WeakReference<Context>(context);
    }

    public static Foo getInstance(Context context) {
        Foo instance = instances.get(context);
        if(instance == null) {
            instance = new Foo(context);
            instances.put(context, instance);
        }
        return instance;
    }
}

```

First of all, will this work? And second, why do I have the feeling that I'm overthinking it?