Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8779205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:43:38+00:00 2026-06-13T19:43:38+00:00

So i have a JList and i am trying to use it inside of

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:43:40+00:00Added an answer on June 13, 2026 at 7:43 pm

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JList that is inside of a JscrollPane and I am trying
I have this JList that I'm using as the display for System.out . I'm
I have a JList inside a Scrollpane. If you click on the list and
I have a JList with a key listener to make it easy for the
I'm making a swing based application where I have a JList that periodically get's
I have a JList nested inside of a JScrollPane. When I add items to
I'm trying to change a JList inside a JScrollPane dynamically, using myList.setListData(someArray); After this
I have a method where i am trying to receive an arraylist from another
I have a JList with a DefaultListModel . How I can make an item
I have Java class with JList and ListSelectionListener: final JList myList = new JList();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.