I want to know if there's a solution to invoke (to force) a method in CellEntity class in order to init each CellEntity in the cellMap?
It is actually possible, but the array has to be created first.
First variation
Create array in constructor first. We cannot change the reference of cellMap after creation, but we still can assign the values within it:
class TestRunner{
public static void main(String[] args){
MapEntity me = new MapEntity(5, 5);
me.initCellMap(); //init cellMap separately
}
}
class MapEntity
{
private final CellEntity[][] cellMap;
public MapEntity(int width, int length){
cellMap = new CellEntity[width][length]; //has to be done in constructor
}
public void initCellMap(){
for(int x=0; x<cellMap.length; x++)
for(int y=0; y<cellMap[0].length; y++)
cellMap[x][y] = new CellEntity();
}
}
Second variation
Almost similar to the first, you create the array first, if you do not want to create it in the constructor (personallyy, I do not favour this approach):
class TestRunner{
public static void main(String[] args){
MapEntity me = new MapEntity();
me.initCellMap(); //init cellMap separately
}
}
class MapEntity
{
final int LENGTH = 5;
final int WIDTH = 5;
final CellEntity[][] cellMap = new CellEntity[WIDTH][LENGTH];
public MapEntity(){
}
public void initCellMap(){
for(int x=0; x<cellMap.length; x++)
for(int y=0; y<cellMap[0].length; y++)
cellMap[x][y] = new CellEntity();
}
}
ICellEntity? Is it a typo error with an extraiinfront?cellMaparray as final?