Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: declaring a "final" variable?

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default declaring a "final" variable?

    Hi, I need help with my homework.

    I'm getting an error along the lines of "local variable names needs to be declared final", first in line 50.


    // Author: Blake Needleman
     
    import javax.swing.*;
    import java.awt.*; 
    import java.awt.event.*;
     
    public class HW9_2 extends JFrame {
    	private JLabel user = new JLabel("Username:");
    	private JTextField username = new JTextField("");
    	private JLabel pass = new JLabel("Password:");
    	private JTextField password = new JTextField("");
    	private JLabel in = new JLabel("Logged in.");
    	private JLabel notIn = new JLabel("Not logged in.");
    	private JButton login = new JButton("Login");
    	private JButton logout = new JButton("Logout");
     
     
    	public static void main(String[] args) {
    		HW9_2 myFrame = new HW9_2(); 
        	myFrame.setVisible(true);
        	myFrame.setTitle("Login");
        	myFrame.setSize(300, 200);
        	myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
     
     
    	public HW9_2() {
    		setLayout(new GridLayout(3, 2, 5, 5));
    		add(user);
    		add(username);
    		add(pass);
    		add(password);
    		add(notIn);
    		add(login);
     
    		String[] names;
    		names = new String[2];
    		names[0] = "Ann";
    		names[1] = "Bob";
    		String[] codes;
    		codes = new String[2];
    		codes[0] = "apple";
    		codes[1] = "blueberry";
     
    		login.addActionListener(new ActionListener() { 
          	public void actionPerformed(ActionEvent e) { 
    			String name = username.getText();
    			String passWord = password.getText();
    			int winning_position;
     
    			for (int i = 0; i < names.length; i ++) {
    				if(name.equals(names[i])) {
    					winning_position = i;
    				}
    			}
     
    			if (passWord.equals(code[winning_position])) {
    				login.setText(logout + "");
    				notIn.setText(in + "");
    			}
     
    		}
     
     
    		});
     
    	}
    }

    Any ideas?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: declaring a "final" variable?

    You're trying to access a local variable within an anonymous inner class.

    Either Initialise String[] names as an instance variable or say:
    final String[] names;

    Hope this helped.

    Edit: Didn't see post date :L
    Last edited by newbie; November 26th, 2010 at 07:57 PM. Reason: Better wording

Similar Threads

  1. Replies: 1
    Last Post: March 31st, 2010, 09:42 PM
  2. "illegal modifier for parameter sum; only final is permitted
    By etidd in forum Java Theory & Questions
    Replies: 3
    Last Post: February 11th, 2010, 07:51 AM
  3. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  4. final class, final <variable> or <data member>
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 20th, 2009, 08:19 AM
  5. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM