Re: Updating GUI & Running Program (AND accessing common variables!)

From: Hal Vaughan (hal_at_thresholddigital.com)
Date: 07/04/04


Date: Sun, 04 Jul 2004 11:50:38 -0400

Jim Sculley wrote:

> Hal Vaughan wrote:
>> Roedy Green wrote:
>>
>>
>>>On Sat, 03 Jul 2004 23:01:37 -0400, Hal Vaughan
>>><hal@thresholddigital.com> wrote or quoted :
>>>
>>>
>>>>While I'm working
>>>>with an anonymous thread called timer, it's not actually timing like
>>>>that. The bigger problem is how can anything inside the "timer" thread
>>>>access variables from outside, in the class "timer" is created within.
>>>
>>>The run in java.util.Timer or the actionPerformed in javax.swing.Timer
>>>has access to all the class and instance variables. They can even
>>>have access to final locals with an anonymous inner class.
>>>
>>>Look at the example. In one I did it with an inner class, and in the
>>>other I used the class itself to contain the method that is run.
>>>
>>>see http://mindprod.com/jgloss/timer.html
>>>
>>
>>
>> Maybe I should rephrase my question, since it seems it works for you, but
>> not always for me.
>>
>> When I try to access variables (specifically strings) that are in the
>> outer class from the inner class, I get a compile error that says I can't
>> do it without finalizing the variable.
> > What could be causing this so I can't access the string variables?
>
>
> You need to clarify what you mean by 'in the outer class'. An inner
> class has access to all *member* variables of the enclosing class.
>
> You can't access *any* non-final *local* variables from an anonymous
> inner class. From a previous post on the topic:
>
> =====================
> An anymous inner class relies on some compiler magic. The compiler
> copies the values of all local variables into corresponding variables of
> the anonymous inner class. The values of these copies can't be kept 'in
> sync' with the values of the originals unless you prevent the original
> from changing. The only way to do this is to make them final. So, to
> make a long story short, an anonymous inner class can only access local
> variables when they are marked final.
> =====================

Okay, let me rephrase, to make sure I understand this correctly.

If I have an outer class, and create an anonymous inner class, that
anonymous inner class cannot access variables from the outer class unless
they're finalized, BUT if I create a NAMED inner class, that class will
have no problem accessing the variables in the outer class.

Is that correct? If so, it finally makes sense to me.

Thanks!

Hal

P.S. I did find a kludge that would let me pass data to an anonymous inner
class -- I found the anon. inner could access Swing components created in
the outer class (I could change the text in a JLabel from an anon inner
class when that label was created in the outer class), and was thinking, if
I had to, I could pass data through a JLabel I could create and never show.
I'm glad I didn't have to resort to that.

> I suspect you are confusing local variables with member variables.
>
> Here's a piece of sample code:
>
> =========================
> public class Outer {
>
> private int variable = 42;
>
> public Outer() {
> //int variable = 42; //not allowed unless variable is marked final
> Inner i = new Inner() {
> void print() {
> System.out.println("From inner: " + variable);
> }
> };
> i.print();
> }
>
> private void print() {
> System.out.println("From outer: " + variable);
> }
>
> public static void main(String[] args) {
> new Outer();
> }
> }
>
> class Inner {
> void print() {}
> }
> ===========================
>
> Jim S.



Relevant Pages

  • Re: Need a way for forgetful developers
    ... Almost every developers define classes and use them in anothers. ... If A is a class and B uses A then we can name B as outer class and A as inner class. ... In its initialize method, outer class should call the initialize method of every inner class objects, ...
    (microsoft.public.vc.language)
  • Re: Trying to create a working internal confirm JOptionPane
    ... inner class, because it is an inner class, belongs to an object of the outer ... That means there is an outer class "this", ... It is not a static variable reference, but a completely different part of the Java language. ... Your mistake was thinking that one part of the Java language should follow rules from another part. ...
    (comp.lang.java.help)
  • Re: Want - but cannot get - a nested class to inherit from outer class
    ... dict dictA: ...   dict membB: ... AM DEFINING METHODS on the inner class. ... proceed with the outer class being the inner class' parent. ...
    (comp.lang.python)
  • Re: Updating GUI & Running Program (AND accessing common variables!)
    ... >>You need to clarify what you mean by 'in the outer class'. ... >>An anymous inner class relies on some compiler magic. ... >>copies the values of all local variables into corresponding variables of ... an anonymous inner class can only access local ...
    (comp.lang.java.gui)
  • Re: Updating GUI & Running Program (AND accessing common variables!)
    ... >anonymous inner class cannot access variables from the outer class unless ... >have no problem accessing the variables in the outer class. ... Finalize is not the same as final. ... have local variables. ...
    (comp.lang.java.gui)