I couldn't save the new things when i tried to pass in new value inside inner class.
The error message is local variable item is accessed from within inner class; needs to declared final. Guide me please. thanks in advance

public class Broadcast{
protected String title;
public Broadcast(String title){
this.title=title;}

public void setTitle (String title){
    	this.title= title;
    }
    public String getTitle (){
    	return title;
    }
}


public class test{
static Broadcast item[] = new Broadcast();
JFrame ammendFilmFrame= new JFrame();
private JButton = ammendTitleButton;

public static void main(String [] args){
ammendFilmFrame.setTitle("Ammend Film menu");
				ammendFilmFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ammendFilmFrame.setSize(240,500);
ammendFilmFrame.setLocationRelativeTo(null);
ammendFilmFrame.setVisible(true);

JPanel ammendFilmPanel= new JPanel(new FlowLayout());

ammendFilmBox[0].add( Box.createRigidArea( new Dimension(12,8)));
			    ammendFilmBox[0].add(ammendTitleButton = new JButton("Title"));
			    ammendTitleButton.setMaximumSize(new Dimension(240,40));
ammendFilmFrame.add( ammendFilmBox[0], BorderLayout.WEST );
ammendTitleButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){

do{
newAmmendTitle = JOptionPane.showInputDialog(null, "Enter new Title For Ammend Process: ", "Ammend Title", JOptionPane.PLAIN_MESSAGE);
if((newAmmendTitle == null) || newAmmendTitle.matches("^\\s*$")){
								JOptionPane.showMessageDialog(null, "ERROR ! \nProcess Interupted. \nPlease Do Not Interupt Program by Clicking Exit Button or Cancel Button.","Error Message", JOptionPane.WARNING_MESSAGE);
							}

							if(newAmmendTitle.length()<=0){
							JOptionPane.showMessageDialog(null, String.format("Please enter valid data to proceed." +
																"\nError: Do not leave BLANK at Title field." +
																"\nPlease Retype the Title Field"), "Ortsa Sdn. Bhd.",JOptionPane.ERROR_MESSAGE);
							}
						}while(newAmmendTitle==null || newAmmendTitle.matches("^\\s*$")|| newAmmendTitle.length()<0);

						item[i].setTitle(newAmmendTitle);
					}
				});
}

Just split "Broadcast" and "test" into 2 files

You create an action listener inside a method, and the code of the listener refers to one of the local variables in that method. The problem is that when the action listener is called, the original method will have finished executing, and its local variables will no longer be available.
If you declare the local variable as "final" then the compiler knows that its value can never change. That means it's safe for the compiler to make a copy of that value for the listener to use after the method has terminated.
Short answer - just do what it said and make the variable final.

Just split "Broadcast" and "test" into 2 files

i actually make them into two files in my assignment. I combined here for you to have better view. I still don't know how to solve on that have to declare final in the inner class. Guide me further. Thanks

You create an action listener inside a method, and the code of the listener refers to one of the local variables in that method. The problem is that when the action listener is called, the original method will have finished executing, and its local variables will no longer be available.
If you declare the local variable as "final" then the compiler knows that its value can never change. That means it's safe for the compiler to make a copy of that value for the listener to use after the method has terminated.
Short answer - just do what it said and make the variable final.

But then, my variable is an array that declared globally. where should i declare final in my code? Thanks for explaining out in detail

I was only able to give a general description because the posted code is incomplete and uncompilable. If you post the complete code and the exact error message (including line number) I'll have a closer look.
J

I was only able to give a general description because the posted code is incomplete and uncompilable. If you post the complete code and the exact error message (including line number) I'll have a closer look.
J

Roughly the idea was like that. Please solve for me. thanks very much.

This is the Broadcast File

public class Broadcast{

    protected String title;

    public Broadcast(){
    	title= null;
    }

	//Constructor with parameters
	public Broadcast(String title){

		this.title= title;
	}

    public void setTitle (String title){
    	this.title= title;
    }
    public String getTitle (){
    	return title;
    }
}

This is the main method class file

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Collection extends JFrame{

	static Broadcast[] item = new Broadcast[100];

	JFrame ammendFilmFrame = new JFrame();
	JButton ammendTitleButton;

	//add details declaration
	String newTitle= null;
	String ammendTitle=null;

	String newAmmendTitle=null;
	String searchItemCode=null;

	public static void main(String [] args){

		item[0]= new Broadcast("Example1");
		ammend(item);
	}

	public void ammend(Broadcast[] item){
		do{
			newTitle = JOptionPane.showInputDialog(null, "Enter New Title: ", "Enter", JOptionPane.PLAIN_MESSAGE);
			if((newTitle == null) || newTitle.matches("^\\s*$")){
				JOptionPane.showMessageDialog(null, "ERROR ! \nProcess Interupted. \nPlease Do Not Interupt Program by Clicking Exit Button or Cancel Button.","Error Message", JOptionPane.WARNING_MESSAGE);
			}
		}while(newTitle==null || newTitle.matches("^\\s*$"));

	    		ammendFilmFrame.setTitle("Ammend Film menu");
				ammendFilmFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ammendFilmFrame.setSize(240,500);
ammendFilmFrame.setLocationRelativeTo(null);
ammendFilmFrame.setVisible(true);

JPanel ammendFilmPanel= new JPanel(new FlowLayout());

Container c = getContentPane();

Box ammendFilmBox[] = new Box[1];
ammendFilmBox[0] = Box.createVerticalBox();

ammendFilmBox[0].add( Box.createRigidArea( new Dimension(12,8)));
ammendFilmBox[0].add(ammendTitleButton =  new JButton ("Title"));

ammendFilmFrame.add( ammendFilmBox[0], BorderLayout.WEST );

ammendTitleButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){

do{
newAmmendTitle = JOptionPane.showInputDialog(null, "Enter new Title For Ammend Process: ", "Ammend Title", JOptionPane.PLAIN_MESSAGE);
if((newAmmendTitle == null) || newAmmendTitle.matches("^\\s*$")){
								JOptionPane.showMessageDialog(null, "ERROR ! \nProcess Interupted. \nPlease Do Not Interupt Program by Clicking Exit Button or Cancel Button.","Error Message", JOptionPane.WARNING_MESSAGE);
							}

if(newAmmendTitle.length()<=0){
							JOptionPane.showMessageDialog(null, String.format("Please enter valid data to proceed." +
																"\nError: Do not leave BLANK at Title field." +
																"\nPlease Retype the Title Field"), "Ortsa Sdn. Bhd.",JOptionPane.ERROR_MESSAGE);
							}
						}while(newAmmendTitle==null || newAmmendTitle.matches("^\\s*$")|| newAmmendTitle.length()<0);

						item[0].setTitle(newAmmendTitle);
					}
				});
	}

}

" and the exact error message (including line number) "

You didn't declare item as a local variable, but you did use it as a parameter name, which is much the same thing, thus masking the class-level variable. Try declaring the parameter as final.

You didn't declare item as a local variable, but you did use it as a parameter name, which is much the same thing, thus masking the class-level variable. Try declaring the parameter as final.

line 68 for which i posted earlier. Error message is local variable is accessed from within inner class; needs to declare final.

i tried

final item[0].setTitle(newAmmendTitle);
final static Broadcast[] item = new Broadcast[100];

But both of the ways doesn't work. So much trouble i faced. Thanks again.

No. I said declare the parameter as final, as in

public void ammend(final Broadcast[] item){

commented: A brief guide, understandable =] +1

No. I said declare the parameter as final, as in

public void ammend(final Broadcast[] item){

Thanks for solving that final declaration. And Sir, if there is a code as such inside the inner class also.

for(int i=0; i<5; i++)
{
item[i].setTitle(newAmmendTitle);
}

Please guide me where should i declare the final for the variable i? Thank you :)

I see no reason to declare i final - in fact it will prevent it being used as a loop variable. That piece of code looks 100% OK to me as far as i is concerned. The final is only needed when an inner class tries to use a local variable declared in the enclosing method.

I see no reason to declare i final - in fact it will prevent it being used as a loop variable. That piece of code looks 100% OK to me as far as i is concerned. The final is only needed when an inner class tries to use a local variable declared in the enclosing method.

Indeed. But in my code when I don't declare i as final. It come out the same problem like the previous one, have to declare final for variable i. I don't know how to declare for the particular code as such

item[i].setTitle(newAmmendTitle);

the variable item had already solved by declaring final at the parameter, how about the variable i? where should i declare in the inner class? Example:

final int i;

Just declare it anywhere in the inner class. It must NOT be declared final, because that means its value will never change, so you can't use it for a loop. The code you posted earlier is good.

for(int i=0; i<5; i++) // i is declared here, in the inner class
 {
    item[i].setTitle(newAmmendTitle);
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.