Calling a constructor from method within the same class

Nathan

I'm new to java and I'm learning about creating object classes. One of my homework assignment requires that I call the constructor at least once within a method of the same object class. I'm getting an error that says The method DoubleMatrix(double[][]) is undefined for the type DoubleMatrix

Here's 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's the method where I try and 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 to the right direction and explain why I'm getting an error?

Vikram

Can someone point me to the right direction and explain why I'm getting an error?

The reason is .. you are not declaring your object correctly. As few answers pointed out, you need to give a keyword called new. This new keyword creates a new object for the class DoubleMatrix in Heap Memory.

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

Hope this helps

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling method of same name from different class

From Java

OOP: Calling a public method within the same class

From Javascript

Calling a class method from the constructor

From Javascript

Calling a method from another method in the same class

From Java

Calling one method from another within same class in Python

From Java

Calling method from constructor

From Java

Calling a method from a class from an element within a list of Objects

From Java

java calling a class from another class within same package

From Java

Calling parent method from within a Thread having the same name

From Java

Calling a method on an Object from within a Class vs from within a method

From Java

Calling super method from within super class

From Dev

Calling Constructor with in constructor in same class

From Dev

Python -- calling a function in same file as a class from within an instance of that class?

From Dev

Calling ES6 class constructor from class static method

From Dev

Calling custom method from within a Ruby core class

From Dev

calling an object method with a setTimeout function from within the same object in Javascript

From Dev

Problems calling a method within the same class in f#

From Dev

Calling a method within the same method?

From Dev

calling a method from same class in xcode

From Dev

Calling a method within the same class

From Dev

Calling virtual method from a constructor

From Dev

Javascript OOP - calling a method from a different method of the same class

From Dev

Error with calling a method from another method in the same class

From Dev

Calling a method from base class to class with same name as the method

From Dev

JS - Calling method in class from other method in same class

From Dev

Calling an object of a method of a class within another method of the same class

From Dev

Calling private method from a Public method in the same class

From Dev

Calling a method from another method inside the same class using getattr

From Dev

javascript why calling a method from another method in the same class need this?

Related Related

  1. 1

    Calling method of same name from different class

  2. 2

    OOP: Calling a public method within the same class

  3. 3

    Calling a class method from the constructor

  4. 4

    Calling a method from another method in the same class

  5. 5

    Calling one method from another within same class in Python

  6. 6

    Calling method from constructor

  7. 7

    Calling a method from a class from an element within a list of Objects

  8. 8

    java calling a class from another class within same package

  9. 9

    Calling parent method from within a Thread having the same name

  10. 10

    Calling a method on an Object from within a Class vs from within a method

  11. 11

    Calling super method from within super class

  12. 12

    Calling Constructor with in constructor in same class

  13. 13

    Python -- calling a function in same file as a class from within an instance of that class?

  14. 14

    Calling ES6 class constructor from class static method

  15. 15

    Calling custom method from within a Ruby core class

  16. 16

    calling an object method with a setTimeout function from within the same object in Javascript

  17. 17

    Problems calling a method within the same class in f#

  18. 18

    Calling a method within the same method?

  19. 19

    calling a method from same class in xcode

  20. 20

    Calling a method within the same class

  21. 21

    Calling virtual method from a constructor

  22. 22

    Javascript OOP - calling a method from a different method of the same class

  23. 23

    Error with calling a method from another method in the same class

  24. 24

    Calling a method from base class to class with same name as the method

  25. 25

    JS - Calling method in class from other method in same class

  26. 26

    Calling an object of a method of a class within another method of the same class

  27. 27

    Calling private method from a Public method in the same class

  28. 28

    Calling a method from another method inside the same class using getattr

  29. 29

    javascript why calling a method from another method in the same class need this?

HotTag

Archive