1

How can I refer to the object on which I'm implementing the instance method. I wrote a class called MatrixMaker that looks like this:

package one;

public class MatrixMaker {

private int rows;
private int columns;

public MatrixMaker(int m, int n){
    rows = m;
    columns = n;
    double[][] matrix = new double[rows][columns];

}

public void printer(){
    for(int i = 0; i < rows; i++){

        for(int j = 0; j < columns; j++){

            System.out.print(matrix[i][j]);
        }
        System.out.println();
    }

}

}

I initialized an object in this class using:

MatrixMaker matrix = new MatrixMaker(3,4);

My question is how do I use

matrix.printer();

to print the array. I can't seem to refer to the contents of the object inside the method printer(). Specifically the line:

System.out.print(matrix[i][j]);
2
  • Define double[][] matrix in your class scope. So place it where private int columns; is. Commented Nov 2, 2014 at 20:53
  • Thank you! I didn't even think about that. Commented Nov 2, 2014 at 20:55

5 Answers 5

2

Your double[][] matrix variable is local to the constructor, so it only exists within the scope of the constructor. Make it an instance variable in order to access it from other methods.

public class MatrixMaker {

private int rows;
private int columns;
private double[][] matrix;

public MatrixMaker(int m, int n){
    rows = m;
    columns = n;
    matrix = new double[rows][columns];

}

This would make it accessible to the printer method. ...

Sign up to request clarification or add additional context in comments.

1 Comment

It worked! Thanks again. I should've thought of that.
2

Your matrix array is a local variable inside the constructor MatrixMaker(int m, int n). If you make it into a member variable you will be able to access it from other methods.

public class MatrixMaker {

    private int rows;
    private int columns;
    private double[][] matrix;

    public MatrixMaker(int m, int n){
        rows = m;
        columns = n;
        matrix = new double[rows][columns];
    }

Comments

2

You defined matrix as a local variable to the constructor of Matrix class. This class won't compile.

Try defining your matrix as a field:

public class MatrixMaker {

    private int rows;
    private int columns;
    private double[][] matrix;

    public MatrixMaker(int m, int n){
        rows = m;
        columns = n;
        matrix = new double[rows][columns];

    }

    public void printer(){
        for(int i = 0; i < rows; i++){
            for(int j = 0; j < columns; j++){
            System.out.print(matrix[i][j]);
        }
        System.out.println();
    }
} 

Comments

2

You have to declare the variable matrix inside your class to make it a member variable, not as local variable in the constructor.

public class MatrixMaker(int m, int n) {
    private int rows;
    private int columns;
    private double[][] matrix;
    ...

Comments

1

Try This:

import java.util.Scanner;

public class MatrixMaker {

private int rows;
private int columns;
double[][] matrix;

public MatrixMaker(int m, int n){
rows = m;
columns = n;
matrix = new double[rows][columns];

}

public void printer(){
  for(int i = 0; i < rows; i++){

    for(int j = 0; j < columns; j++){

        System.out.print(matrix[i][j]+"  ");
    }
    System.out.println();
}

}

public static void main(String[] args) {
  MatrixMaker m=new MatrixMaker(4,4);
  Scanner in=new Scanner(System.in);
  System.out.println("Enter Matrix Elements:");
  for(int i=0;i<m.rows;i++){
    for(int j=0;j<m.columns;j++)
        m.matrix[i][j]=Integer.parseInt(in.next());
      }

   in.close();

  m.printer();
}

}

Provide Input in The Console as Follows:

1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

Or you may Provide Input numbers one by one,as: 1 2 3 4 5 6 ..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.