File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes confusion about inner class and refering to an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "confusion about inner class and refering to an object" Watch "confusion about inner class and refering to an object" New topic

confusion about inner class and refering to an object

Gulsum Ramazanoglu
Ranch Hand

Joined: Jun 27, 2008
Posts: 51
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;
<pre name="code" class="core">
JPanel yadayada=new JPanel(new BorderLayout());

.. 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) ;

<pre name="code" class="core">
Action save;
save=new AbstractAction()
public void actionPerformed(ActionEvent e)


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

" 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.
Manuel DeAngelo

Joined: Jan 25, 2008
Posts: 13
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
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

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.

[Jess in Action][AskingGoodQuestions]
Gulsum Ramazanoglu
Ranch Hand

Joined: Jun 27, 2008
Posts: 51
Manuel, Ernest, thank you very much.. now I got the point and its ok
Thanks again..
jQuery in Action, 2nd edition
subject: confusion about inner class and refering to an object
Similar Threads
Conditional (Ternary) Operator (?:)
doubt on object
Anonymous Inner Class
StringBuffer / StringBuilder methods
Customizing JToolTip