First, here is the relevant code:
public class AtCounter
{
private char[][] atMat;
private int atCount;
public AtCounter()
{
atCount=0;
atMat= new char[][]{{'@','-','@','-','-','@','-','@','@','@'},
{'@','@','@','-','@','@','-','@','-','@'},
{'-','-','-','-','-','-','-','@','@','@'},
{'-','@','@','@','@','@','-','@','-','@'},
{'-','@','-','@','-','@','-','@','-','@'},
{'@','@','@','@','@','@','-','@','@','@'},
{'-','@','-','@','-','@','-','-','-','@'},
{'-','@','@','@','-','@','-','-','-','-'},
{'-','@','-','@','-','@','-','@','@','@'},
{'-','@','@','@','@','@','-','@','@','@'}};
}
public int countAts(int r, int c)
{
if(r<atMat.length && c<atMat[0].length && r>=0 && c>=0 && atMat[r][c]=='@')
{
atMat[r][c]='X';
atCount++;
}
else
if(r-1<atMat.length && r-1>=0)
countAts(r-1,c);
else
if(r+1<atMat.length && r+1>=0)
countAts(r+1,c);
else
if(c-1<atMat[0].length && c-1>=0)
countAts(r,c-1);
else
if(c+1<atMat[0].length && c+1>=0)
countAts(r,c+1);
return atCount;
}
Note that the countsAts
method contains the line atMat[r][c]='X';
, which changes the value of an element within the two-dimensional array. Despite this fact, on the line private char[][] atMat;
, Netbeans gives me the warning "Field atMat can be final". Why would this field be able to be final if I am modifying its value? Does it have something to do with where in the code I am currently modifying it? Is Netbeans bugged (doubt it)? If someone could explain the warning I would be very grateful!
You can change the contents of the object that is referenced by the name atMat. atMat is a reference. The reference itself can be made final.