• Post Reply Bookmark Topic Watch Topic
  • New Topic

local variable i defined in an enclosing scope must be final or effetively final?  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys this is the first time I have ever come across this error,what does it mean and how do I go about solving this issue?

thanks


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Keep in mind that the scopes are different. Local variables are only in scope for the lifetime of the method call. On the other hand, instances, even those of anonymous inner classes, are in scope while they are reachable.  So.... it should never be allowed for one to access the other.

Java, interestingly, does allow it. And of course, it is *not* actually allowing it, but instead, making a copy of the value for the instance. However, to do this, it must make sure that the value doesn't change (ie. a constant). So, you must guarantee that the value has been set, and ... that it is declared as final or effectively final.

Henry
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Henry

also have a side question




for some reason when I click on a button even if it is set to "X" or "O" the button still changes

this block of code doesn't do what I expect it to do
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're referencing a non-final local variable from a local class. In this particular case, your actionPerformed() method references i, but the value of i changes over time.

The question is, what is the value of i by the time the button is clicked? Does i even exist anymore by the time the button is clicked?

You can solve this simply by having an effectively final reference to the button you want to interact with:
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that == compares references and equals() compares values.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is my updated code but it doesn't do what I want it to do,when I click a button it changes to "X" but then I try to click the next button but it doesn't change it just remains "*" even though I am breaking out of the loop and entering a new loop each time the button is pressed?

 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's really bad practice to perform logic based on the state of your user interface. You need a class that represents your game of Tic Tac Toe, and change the way your button behaves based on an object of that class, rather than the current value of the button's text attribute.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Stephan for the advice I will rewrite this program for sure,

just a question I also replaced break with return yet when I click another button nothing happens it remains "*"

thanks
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a few typos messy programming on my part sorry,

anyway the code is very sloppy and poorly written so how would I go about implementing it the way you are saying Stephan? any pointers would be great =)

thanks also here is the updated code


 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop thinking about the GUI, and start with writing a class that represents a game of Tic Tac Toe. What does a game of Tic Tac Toe consist of? What can you do with it?
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
consists of x's and o's also consists of a 3 x 3 board and two players,you can win if you get three in a row,

so I should make a command line version first and centre it around that?

thanks
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not necessarily, you can make a graphic interface before you make a command line interface, but in either case you first need a good model of your problem domain. You don't even need to implement it, but you should definitely start by designing a public API. As a matter of fact, let's do that now.

Describe in detail the things that are involved in playing a game of Tic Tac Toe. Describe what you can do with each of those things. Don't write any code, just make a description of the problem, use a paragraph for each thing you describe.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll give it a shot =)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!