LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-05-2006, 08:46 PM   #1
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
java: inner class, variable accessibility problem


edit: sorry for the big indentations, looks fine maximized though
Code:
for (int y= 0; y < HEIGHT; y++)
		{
			for (int x = 0; x < WIDTH; x++)
			{
				hide[x][y] = '0';
				show[x][y] = new JButton(x +","+y);
				show[x][y].addMouseListener(new MouseAdapter()
				{
					public void mouseClicked(MouseEvent e)
					{
						check(x,y); //problem here
					}
				});
				show[x][y].setSize(500,100);
				frame.getContentPane().add(show[x][y]);	
			}						
		}
error message (in eclipse IDE): "Cannot refer to a non-final variable x inside an inner class defined in a different method"

as the error message states, making the x and y variables 'final' solves the problem, however, if they are final then they wouldnt be able to be incremented on each iteration of the for loop.

if i put this inner class in its own file and then applied it here then i wouldnt be able to access the x and y variables. i could overload the mouseClicked function with parameter list (MouseEvent e, int x, int y) but it wouldnt get called.

i havent tried it but maybe making the variables either global or static would solve it, but i dont want to have to resort to that.

any help appreciated thanks

Last edited by nadroj; 10-05-2006 at 08:47 PM.
 
Old 10-05-2006, 09:01 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
After this line try this:

show[x][y] = new JButton(x +","+y);

final int fx = x;
final int fy = y;

Use fx and fy in the inner class.
 
Old 10-05-2006, 09:18 PM   #3
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60
it works, never thought of that. also not too much overhead because of the small scope it has.
thanks crabboy
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
php4: problems with variable access in a class method eantoranz Programming 5 04-26-2006 07:52 AM
C++: difference between static method variable and static class variable? Thinking Programming 3 01-16-2006 10:08 AM
C++ / Passing a variable by reference to a class member function. sepulture Programming 12 11-15-2005 10:23 PM
Pass object variable to anothor class in JAVA hus Programming 2 05-11-2005 04:04 AM
Python how name variable inside a class - very simple? lugoteehalt Programming 5 10-22-2003 05:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration