For Programmers: Free Programming Magazines  


Home > Archive > Java Help > July 2004 > accessing local variables from inner class









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author accessing local variables from inner class
Allan Bruce

2004-07-21, 3:59 pm

I have a lot of variables which are declared locally to a method in my GUI.
I want to add an ActionListener and be able to access these variables. At
the moment, I am having to make them class member variables to access them,
is there a way to avoid this? Here is an example...

public class myClass
{
private int mTest = 100;

// ....

doSomething()
{
JDialog lDialog = new JDialog();

// ...

JButton lCancelButton = new JButton("Cancel");
lCancelButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
mTest = 200; // can do this as mTest is a member variable
lDialog.hide(); // cant do this because lDialog is not a
member variable
}
});
}
}


It would be very messy to make my JDialog variable a member, as I have a lot
of these to add to my class. Any ideas how to get around this?
Thanks,
Allan


Murray

2004-07-21, 3:59 pm


"Allan Bruce" <allanmb@TAKEAWAYf2s.com> wrote in message
news:cdllab$gl3$1@news.freedom2surf.net...
> I have a lot of variables which are declared locally to a method in my

GUI.
> I want to add an ActionListener and be able to access these variables. At
> the moment, I am having to make them class member variables to access

them,
> is there a way to avoid this? Here is an example...
>
> public class myClass
> {
> private int mTest = 100;
>
> // ....
>
> doSomething()
> {
> JDialog lDialog = new JDialog();
>
> // ...
>
> JButton lCancelButton = new JButton("Cancel");
> lCancelButton.addActionListener(new

java.awt.event.ActionListener()
> {
> public void actionPerformed(ActionEvent evt)
> {
> mTest = 200; // can do this as mTest is a member

variable
> lDialog.hide(); // cant do this because lDialog is not

a
> member variable
> }
> });
> }
> }
>
>
> It would be very messy to make my JDialog variable a member, as I have a

lot
> of these to add to my class. Any ideas how to get around this?
> Thanks,
> Allan


You can access variables outside of the inner class if you declare them as
final


Adam

2004-07-21, 3:59 pm


"Allan Bruce" <allanmb@TAKEAWAYf2s.com> wrote in message
news:cdllab$gl3$1@news.freedom2surf.net...
> I have a lot of variables which are declared locally to a method in my

GUI.
> I want to add an ActionListener and be able to access these variables. At
> the moment, I am having to make them class member variables to access

them,
> is there a way to avoid this? Here is an example...
>
> public class myClass
> {
> private int mTest = 100;
>
> // ....
>
> doSomething()
> {
> JDialog lDialog = new JDialog();
>
> // ...
>
> JButton lCancelButton = new JButton("Cancel");
> lCancelButton.addActionListener(new

java.awt.event.ActionListener()
> {
> public void actionPerformed(ActionEvent evt)
> {
> mTest = 200; // can do this as mTest is a member

variable
> lDialog.hide(); // cant do this because lDialog is not

a
> member variable
> }
> });
> }
> }
>
>
> It would be very messy to make my JDialog variable a member, as I have a

lot
> of these to add to my class. Any ideas how to get around this?
> Thanks,
> Allan
>
>


Yes, by making the variables final. This will however mean you can't
re-assign them, so mTest=200 won't compile if you make it final, but
final JDialog lDialog = new JDialog();
will allow you to call lDialog.hide() from the inner class.

adam


Allan Bruce

2004-07-21, 3:59 pm

>
> Yes, by making the variables final. This will however mean you can't
> re-assign them, so mTest=200 won't compile if you make it final, but
> final JDialog lDialog = new JDialog();
> will allow you to call lDialog.hide() from the inner class.
>
> adam
>
>


Thanks, is there any other problems with final? Especially if I were to
have multiple instances of this class?
Thanks,
Allan


Murray

2004-07-21, 3:59 pm


"Allan Bruce" <allanmb@TAKEAWAYf2s.com> wrote in message
news:cdln7e$gp9$1@news.freedom2surf.net...
>
> Thanks, is there any other problems with final? Especially if I were to
> have multiple instances of this class?
> Thanks,
> Allan


The only impact that declaring a variable final has is that you can only
ever assign it a value once. Apart from that, final local variables will
behave exactly the same as non-final local variables. final instance
variables however have the additional contraint that they *must* be assigned
a value, and it must be done when they're declared, or in the constructor(s)
of the class. static final variables must be assigned a value when they are
declared.

Using final can actually improve performance sometimes. If the JVM knows
that a value can't change, it can sometimes make optimisations. But I
wouldn't advise going and making all your variables final, you'll lose
flexibility and except for rare cases, the performance benefit will be
negligible


Murray

2004-07-21, 3:59 pm

> static final variables must be assigned a value when they are
> declared.


(or in a static initialiser)


Dale King

2004-07-28, 9:08 pm

"Allan Bruce" <allanmb@TAKEAWAYf2s.com> wrote in message
news:cdln7e$gp9$1@news.freedom2surf.net...
>
> Thanks, is there any other problems with final? Especially if I were to
> have multiple instances of this class?



When the inner class access a final variable it really doesn't access the
variable itself. What happens underneath is that the inner class gets a
member variable into which is copied the value that the variable had when
the class was created.
--
Dale King


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com