A constructor call from a method within the same class

I am new to java and I will learn about creating object classes. One of my homework tasks requires me to call the constructor at least once in a method of the same object class. I get an errorThe method DoubleMatrix(double[][]) is undefined for the type DoubleMatrix

Here is my constructor:

public DoubleMatrix(double[][] tempArray)
{
    // Declaration
    int flag = 0;
    int cnt;

    // Statement

    // check to see if doubArray isn't null and has more than 0 rows
    if(tempArray == null || tempArray.length < 0)
    {
        flag++;
    }

    // check to see if each row has the same length
    if(flag == 0)
    {
        for(cnt = 0; cnt <= tempArray.length - 1 || flag != 1; cnt++)
        {
            if(tempArray[cnt + 1].length != tempArray[0].length)
            {
                flag++;
            }
        }
    }
    else if(flag == 1)
    {
        makeDoubMatrix(1, 1);// call makeDoubMatrix method
    }

}// end constructor 2

Here is the method in which I try to call the constructor:

public double[][] addMatrix(double[][] tempDoub)
{
    // Declaration
    double[][] newMatrix;
    int rCnt, cCnt;

    //Statement

    // checking to see if both are of same dimension
    if(doubMatrix.length == tempDoub.length &&
            doubMatrix[0].length == tempDoub[0].length)
    {
        newMatrix = new double[doubMatrix.length][doubMatrix[0].length];

        // for loop to add matrix to a new one
        for(rCnt = 0; rCnt <= doubMatrix.length; rCnt++)
        {
            for(cCnt = 0; cCnt <= doubMatrix.length; cCnt++)
            {
                newMatrix[rCnt][cCnt] = doubMatrix[rCnt][cCnt] + tempDoub[rCnt][cCnt];
            }
        }
    }
    else
    {
        newMatrix = new double[0][0];
        DoubleMatrix(newMatrix)
    }

    return newMatrix;
}// end addMatrix method

Can someone point me in the right direction and explain why I get an error message?

+4
source share
3 answers

Can someone point me in the right direction and explain why I get an error message?

, .. . , new. new DoubleMatrix .

else { newMatrix = new double[0][0]; new DoubleMatrix(newMatrix) }

,

+5

, , - . , . - contructore, java- super()

+3

You can use this()to call the constructor from inside another constructor (or super()to call the constructor of the parent class), but you cannot use the constructor from another method. Maybe you wanted to create a new object? If yes, just use new Object();. The constructor will be called for the new object.

+2
source

All Articles