View Single Post
Old Sep 18th, 2007, 9:52 PM   #6
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 908
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Okay, you have another (big) problem: When you name a parameter of a method the same as an instance variable of that class, the parameter variable "hides" the instance variable. So, in this snippet of code, for example:

public class GridNode
{
    private int[][] state;
    
    ...
    
    public void moveRight(int[][] state) {
        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 3; col++) {
                if (state[row][col] == 0) {
                    state[row][col] = state[row][col+1];
                    state[row][col+1] = 0;
                    return;
                }
            }
        }
    }
    
    ...
    
}
The state parameter, not the state instance variable, is being changed! As you can image, all sorts of problems will arise. (The original array, presumably declared in NewMain, will be altered, for the reasons described in my last post.) The solution: Either don't name any of your parameters the same as any of your instance variables or use the this keyword to qualify variables:

this.state[row][col] = state[row][col+1];
All that said, however, this is not the cause of your problem--and perhaps you intended that behavior. If so, the method should be declared static (and you might want to consider changing the parameter name anyway for obvious reasons).

Last edited by titaniumdecoy; Sep 18th, 2007 at 10:11 PM.
titaniumdecoy is offline   Reply With Quote