Proyb2
Proyb2

Reputation: 993

WeakReference symbol cannot be found

Already import the weakreference but the compiler cannot find the symbol, what wrong? There a memory leak in DumpReceiver.java I thought weakreference might free after used?

import java.lang.ref.WeakReference;

Receiver r = new DumpReceiver(System.out);
WeakReference<Receiver> wr = new WeakReference<DumpReceiver>(r);


MidiInDump.java:64: cannot find symbol
symbol  : constructor WeakReference(javax.sound.midi.Receiver)
location: class java.lang.ref.WeakReference<DumpReceiver>
                WeakReference<Receiver> wr = new WeakReference<DumpReceiver>(r);

                                             ^

Upvotes: 1

Views: 724

Answers (1)

Joachim Sauer
Joachim Sauer

Reputation: 308249

Look closely at the error message. It's not talking about the class (it's finding that just fine). It is talking about the constructor. It doesn't find a constructor that takes a javax.sound.midi.Receiver argument on the type WeakReference<DumpReceiver>. Looking at the JavaDoc of WeakReference<T> there is one constructor that takes an argument of type T.

You're trying to create a WeakReference<DumpReceiver> but try to pass in an object of type javax.sound.midi.Receiver. You either need to create a WeakReference<Receiver> instead or change the variable r to be of type DumpReceiver.

Upvotes: 4

Related Questions