Tell me, please, how is it possible to initialize the matrix in the init() method of the Matrix class so that the rows and columns have values ​​from the main method? It turns out that the constructor of the RunnableTask parent class is immediately called, then it goes to the Matrix class’s init() method and the matrix has a size of zero. And it is necessary that in the init() method of the Matrix class, the matrix has a size of 5*5 .

 public static void main(String[] args) { RunnableTask matrix = new Matrix(5, 5); } public abstract class RunnableTask { public abstract void runTask(); public abstract void init(); public void run() { runTask(); } public RunnableTask() { init(); } } public class Matrix extends RunnableTask { int rows; int cols; int matrix[][]; public Matrix(int rows, int cols) { this.rows = rows; this.cols = cols; } @Override public void init() { matrix = new int[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix[i][j] = i * j; } } } @Override public void runTask() { for (int i = 0; i < rows; i++) { System.out.println(); for (int j = 0; j < cols; j++) { System.out.print(" " + matrix[i][j] + " "); } } } } 
  • Can remove the init method from RunnableTask ? In essence, it duplicates the constructor’s functionality. - diraria
  • Unfortunately, you cannot remove the init () method. Here I am trying to understand, is it possible? Or just joked, giving such a test. - user266161
  • initialization in runTask () is also not allowed - user266161
  • In general, it is a very bad practice to call redefinable methods from the parent's constructor. first the constructor of the parent class will be called, and then only the constructor of the heir and there may be unpredictable behavior (as in this example). I don’t know how much you can change the code in your task, but I would suggest passing rows, cols - damintsew through constructors
  • Initialization of the matrix and filling should occur in the init () method of the Matrix class. Unfortunately, it is impossible to change. It would be possible, already decided. - user266161

1 answer 1

The constructor of the parent class is called before the execution of the statements of the dependent class. In this case, the constructor of the RunnableTask class will be called before the dimensions of the matrix are specified. Therefore, only 3 possible solutions:

1 Call init () in the Matrix constructor:

 public Matrix(int rows, int cols) { this.rows = rows; this.cols = cols; init(); } 

2 Since init is declared public, you can initialize the RunnableTask after creating the object

  public static void main(String[] args) { RunnableTask matrix = new Matrix(5, 5); matrix.init(); matrix.run(); } 

3 Sharpen the RunnableTask class under the matrix, for example, using the RunnableMatrixTask inheritance

  • DaysLikeThis thank you very much. It fits the condition of the problem and works as it needs the first option. Thank. - user266161
  • I wanted to vote, but I can not yet, the rating (reputation) is not enough - user266161
  • @java_jun You can mark response as a solution - DaysLikeThis