Java: prevent an object reference from being used outside a method
<p>I've defined an interface <code>RecordVisitor</code>, declared as follows:</p>

<pre><code>public interface RecordVisitor&lt;K, V, Result&gt; {
    public Result visit(Record&lt;K, V&gt; rec);
}
</code></pre>

<p>The idea is that implementations of <code>visit</code> will get called with a Record object, do their logic, and return a result.  I would like to prevent implementations from stashing away the <code>rec</code> value and using it outside of the <code>visit</code> invocation.</p>

<p>The only way I've thought to do this is by adding state to <code>Record</code>, and throwing <code>IllegalStateException</code> if any method is called on the record object while it is not "active".  Then I would write dire warnings into the javadoc, and hope that implementers read it.</p>

<p>Is there a more robust way to prevent using the <code>Record</code> object from being used outside of the <code>visit</code> method?  If possible, I'd like to build an interface that results in compile-time errors if the contract is violated.</p>
