Re: Updating GUI & Running Program (AND accessing common variables!)
From: Hal Vaughan (hal_at_thresholddigital.com)
Date: 07/04/04
- Next message: Roedy Green: "Re: Updating GUI & Running Program (AND accessing common variables!)"
- Previous message: Jim Sculley: "Re: Updating GUI & Running Program (AND accessing common variables!)"
- In reply to: Jim Sculley: "Re: Updating GUI & Running Program (AND accessing common variables!)"
- Next in thread: Roedy Green: "Re: Updating GUI & Running Program (AND accessing common variables!)"
- Reply: Roedy Green: "Re: Updating GUI & Running Program (AND accessing common variables!)"
- Reply: Jim Sculley: "Re: Updating GUI & Running Program (AND accessing common variables!)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Roedy Green: "Re: Updating GUI & Running Program (AND accessing common variables!)"
- Previous message: Jim Sculley: "Re: Updating GUI & Running Program (AND accessing common variables!)"
- In reply to: Jim Sculley: "Re: Updating GUI & Running Program (AND accessing common variables!)"
- Next in thread: Roedy Green: "Re: Updating GUI & Running Program (AND accessing common variables!)"
- Reply: Roedy Green: "Re: Updating GUI & Running Program (AND accessing common variables!)"
- Reply: Jim Sculley: "Re: Updating GUI & Running Program (AND accessing common variables!)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|