• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

confusion about inner class and refering to an object

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody, I have again one more q... conceptually I couldnt understand a point.. if you enlighten me about it, I will be very happy.

In the constructor of a class, I create an object with;
<blockquote>code:
<pre name="code" class="core">
JPanel yadayada=new JPanel(new BorderLayout());
</pre>
</blockquote>

.. and right a couple of lines down there, I place an action for a button which is making the panel unvisible(thats what I intended) ;

<blockquote>code:
<pre name="code" class="core">
Action save;
save=new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
daily_details_table_panel.setVisible(false);
}
};

</pre>
</blockquote>

...and the compiler "doesnt give concent" :roll: by saying;

"editing.java:77: local variable daily_details_table_panel is accessed from within inner class; needs to be declared final
{ daily_details_table_panel.setVisible(false);"

Conceptually I didnt get the point... Do you know why?
Thank you all.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is, that you define the JPanel in the constructor, if you would define it as member variable you would not have that problem.

But actually I can't give you an answer why it behaves like this.

But in general it is better practice to make member variables and create methods, not compute everything in the constructor.

regards, Manuel
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Initializing things in your constructor is fine, and while using a member variable would fix the error, it would lead to a class containing a bunch of unneeded member variables, and who needs that?

The error message says to mark your variable declaration "final" -- why not just do that?

final JPanel daily_details_table_panel = new JPanel(new BorderLayout());

The explanation is simple: for an inner class to be able to access a local variable, the compiler needs to be sure the variable's value will be available when the inner class executes -- which may be long after the method in which the local variable is declared, has returned. Therefore the compiler insists that the variable be final (constant) so that it can save a copy of the variable's value for use by the inner class.
 
Gulsum Ramazanoglu
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manuel, Ernest, thank you very much.. now I got the point and its ok
Thanks again..
 
Let's go to the waterfront with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic