Search code examples
javaswingjbuttonjlistfinal

inside JButton's actionperformed final variables required?


So i have a JList and i am trying to use it inside of a JButtons actionPerformed method and it is asking me to make the JList final why is that, below is a code snippet

public SomeClass() {    
  btnNewButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
         list.clearSelection();             
    }});
}

I don't actually have a problem making it final, I just am not sure why i would need to.


Solution

  • To answer your question, you need to understand the basics, as to how the JVM use to work. When the classes are compiled which contain inner classes, the byte code which gets generated does not actually implement inner classes as a class within a class.

    WHY THE ERROR : The local variable was accessed from an inner class, needs to declare it final

    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JMenu;
    import javax.swing.JPanel;
    
    public class foo extends JPanel
    {  
      public foo()
      {
        final JMenu edit = new JMenu();
        edit.getItem(0).addMouseListener(new MouseAdapter(){ 
        @Override
            public void mouseClicked(MouseEvent e) 
            {
                if (e.getClickCount() == 1) {
                    edit.getItem(0).setEnabled(true);
                }
            } 
        });
      }
    }
    

    When you compile your this program, two files will be created, Foo.class and Foo$1.class. So now your problem comes, since the Second class i.e. foo$1.class doesn't know that Variable edit is present inside the First class i.e. foo.class.

    So how to solve this problem ? What JVM does, is that, It makes a requirement for the developer to make the variable of an outer class to be declared as final.

    Now this being done, now JVM quietly places a hidden variable with the name val$edit inside the 2nd compiled class file, here is the output as got from javap

    Ouput for foo.class

    C:\Mine\JAVA\J2SE\folder>javap foo.class
    Compiled from "foo.java"
    public class foo extends javax.swing.JPanel {
      public foo();
    }
    

    Now since, edit is local to the constructor, hence the output as above.

    C:\Mine\JAVA\J2SE\folder>javap foo$1.class
    Compiled from "foo.java"
    class foo$1 extends java.awt.event.MouseAdapter {
      final javax.swing.JMenu val$edit;
      final foo this$0;
      foo$1(foo, javax.swing.JMenu);
      public void mouseClicked(java.awt.event.MouseEvent);
    }
    

    The Variable val$edit is assigned the same value which has been assigned to edit since now the compiler knows that the value cannot be changed as it has been declared final and hence it works this time.

    Now what if I change the edit Variable from being Local to being Instance. Now the object of the class knows everything about this variable edit, if it gets changed. So changing the above program likewise we get :

    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JMenu;
    import javax.swing.JPanel;
    
    public class foo extends JPanel
    {  
        JMenu edit = new JMenu();
    
        public foo()
        {   
            edit.getItem(0).addMouseListener(new MouseAdapter(){ 
            @Override
                public void mouseClicked(MouseEvent e) 
                {
                if (e.getClickCount() == 1) {
                        edit.getItem(0).setEnabled(true);
                    }
                } 
            });
        }
    }
    

    Here in this case, we are not suppose to declare and define it as being final, because in this case since the Variable being Local to the whole class, the Variable is send to the Inner Class along with the Object Reference i.e. this

    C:\Mine\JAVA\J2SE\folder>javap foo.class
    Compiled from "foo.java"
    public class foo extends javax.swing.JPanel {
      javax.swing.JMenu edit;
      public foo();
    }
    

    Here is how the Variable is send in this case i.e. this$0 :

    C:\Mine\JAVA\J2SE\folder>javap foo$1.class
    Compiled from "foo.java"
    class foo$1 extends java.awt.event.MouseAdapter {
      final foo this$0;
      foo$1(foo);
      public void mouseClicked(java.awt.event.MouseEvent);
    }
    

    Seems like that the interpretation, how this situation works, according to me. Just now I found this wonderful explanation on the internet regarding Mystery of Accessibility in Local Inner Classes, might be this will help you understand the situation in a much better way :-)