Maven local variable “needs to be declared final” where no variable exists or where variable is already final Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Maven local variable “needs to be declared final” where no variable exists or where variable is already final without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am attempting to compile my program with the goals clean package shade:shade; however, I run into this issue:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project core: Compilation failure
[ERROR] /C:/Users/justi/Programming/YAML to Bot/YamlToBot/core/src/main/java/com/justinschaaf/yamltobot/core/setup/[89,71] local variable module is accessed from within inner class; needs to be declared final

From my research, the usual solution is to declare the variable as final, but there is not a variable at line 71 and the variable at line 89 is already final, as can be seen by, inserted below.

package com.justinschaaf.yamltobot.core.setup;

import java.awt.Color;
import java.awt.Desktop;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

import com.justinschaaf.yamltobot.core.common.Module;
import com.justinschaaf.yamltobot.core.common.Reference;
import com.justinschaaf.yamltobot.core.common.VersionChecker;
import com.justinschaaf.yamltobot.core.handler.ConfigHandler;

 * The primary class for setting up the window for YamlToBot
 * @author Justin Schaaf
 * @since 1.0.0
public class SetupWindow extends JFrame {

    private JPanel contentPane;

     * The primary function for setting up the window
     * @since 1.0.0
    public SetupWindow(Module module) {
        setTitle("YamlToBot | " + ConfigHandler.getString("name", module.getName()));
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[]{0, 0};
        gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
        gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};

        JPanel header = new JPanel();
        FlowLayout flowLayout = (FlowLayout) header.getLayout();
        header.setForeground(new Color(Integer.parseInt("FEFEFE",16)));
        header.setBackground(new Color(Integer.parseInt("283F50",16)));
        GridBagConstraints gbc_header = new GridBagConstraints();
        gbc_header.anchor = GridBagConstraints.NORTH;
        gbc_header.gridheight = 2;
        gbc_header.fill = GridBagConstraints.HORIZONTAL;
        gbc_header.gridx = 0;
        gbc_header.gridy = 0;
        contentPane.add(header, gbc_header);

        JLabel label = new JLabel("");
        label.setIcon(new ImageIcon(SetupWindow.class.getResource("/assets/logo.png")));

        final JTextArea log = new JTextArea();
        log.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent arg0) {

                try {

                    final File logFile = new File(module.getDir() + "logs/log-latest.log");
                    BufferedReader logText = new BufferedReader(new InputStreamReader(new FileInputStream(logFile)));

                    StringBuilder text = new StringBuilder();
                    String line;

                    while ((line = logText.readLine()) != null) {
                    text.append(line + "n");


                } catch (FileNotFoundException e) {
                } catch (IOException e) {

        log.setFont(new Font("Monospaced", Font.PLAIN, 18));
        log.setText("Move the cursor here to update the log!");
        GridBagConstraints gbc_log = new GridBagConstraints();
        gbc_log.gridheight = 3;
        gbc_log.fill = GridBagConstraints.BOTH;
        gbc_log.gridx = 0;
        gbc_log.gridy = 2;
        contentPane.add(log, gbc_log);

        JPanel footer = new JPanel();
        FlowLayout flowLayout_1 = (FlowLayout) footer.getLayout();
        footer.setForeground(new Color(Integer.parseInt("FEFEFE",16)));
        footer.setBackground(new Color(Integer.parseInt("8693A4",16)));
        GridBagConstraints gbc_footer = new GridBagConstraints();
        gbc_footer.anchor = GridBagConstraints.SOUTH;
        gbc_footer.fill = GridBagConstraints.HORIZONTAL;
        gbc_footer.gridx = 0;
        gbc_footer.gridy = 5;
        contentPane.add(footer, gbc_footer);

        final JTextPane version = new JTextPane();
        version.setForeground(new Color(Integer.parseInt("FEFEFE",16)));
        version.setBackground(new Color(Integer.parseInt("8693A4",16)));
        version.setFont(new Font("Tahoma", Font.BOLD, 15));

        final JTextPane update = new JTextPane();
        update.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent arg0) {
                if (VersionChecker.isUpdated() == false) {
                    try {
                        Desktop.getDesktop().browse(new URL(Reference.releasesURL).toURI());
                    } catch (MalformedURLException e) {
                    } catch (IOException e) {
                    } catch (URISyntaxException e) {
        update.setForeground(new Color(Integer.parseInt("FEFEFE",16)));
        update.setBackground(new Color(Integer.parseInt("8693A4",16)));
        update.setFont(new Font("Tahoma", Font.PLAIN, 18));

     * Checks if the jar is updated and returns text accordingly
     * @return The text stating whether or not the jar is up to date
     * @since 1.0.0
    private String updateText() {

        if (VersionChecker.isUpdated() == false) return "A new update is available: " + VersionChecker.getLatestVersion();
        if (Reference.prerelease == true) return "You are using a pre-release build.";
        else return "Your jar is up to date.";



Line 71 is gbc_header.anchor = GridBagConstraints.NORTH;, and line 89 is final File logFile = new File(module.getDir() + "logs/log-latest.log");

Thank you in advance for your time.


If you look closely at the error[89,71] local variable module is accessed from within inner class; needs to be declared final

It tells that the variable named module is not final. You can’t use a non-final local variable that is declared outside the anonymous inner class, inside that inner class.

You are using module in line 89 as module.getDir().

Also, 89 is the line number, 71 is the column or character number in that line.

To fix it, change public SetupWindow(Module module) to public SetupWindow(final Module module).

We are here to answer your question about Maven local variable “needs to be declared final” where no variable exists or where variable is already final - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji