Search code examples
javafits

I'm trying to call method from another class but it is undefined


I've created a method sReadFitsData within the class ReadFitsData. I want to call this method in any class. For example, from the class TestRead. But instead I've got a compilation error: "The method sReadFitsData(String) is undefined for the type TestRead".

Here is my code:

ReadFitsData.java

package readFits;

import java.io.IOException;

import nom.tam.fits.BasicHDU;
import nom.tam.fits.Fits;
import nom.tam.fits.FitsException;
import nom.tam.fits.Header;

public class ReadFitsData {

public int[][][] sReadFitsData(String fitsFileName) throws IOException, FitsException {
    int[][][] myData;
    Fits f; // fits object
    Header hdr;
    Object fData;

    try {
        f = new Fits (fitsFileName); 
    } catch (FitsException fEx) {
        throw new IOException ("Failed to open FITS file; "+fEx.getMessage());
    }

    try {
        BasicHDU hdu = f.getHDU(0); // 0 is for first header, 1 is for second one.
        f.close();
        //hdr = hdu.getHeader();

        //int size = (int) hdr.getDataSize();

        fData = hdu.getData().getData(); // Object fData
    } catch (FitsException fEx) {
            throw new IOException ("Failed to get Data; "+fEx.getMessage());
    }


    if (!fData.getClass().isArray()) {
        throw new IOException ("Unknown HDU Data type: " + fData.getClass().getName());// + fEx.getMessage());
    }

    myData = (int[][][]) fData; // cast the fData to an int[][][]

    return myData;
}

}

Another class for call this method:

TestRead.java

package readFits;

public class TestRead {
    public static void main(String[] args){
        String fname = "myFitsFile.fits";
        int[][][] arr = sReadFitsData(fname); // here is the compilation error.
    }

}

ADDITION according to the answer:

First I did my method sReadFitsData as public static for independence of objects.

Second I made an import of a class:

TestRead.java

import projectName.readFits.ReadFitsData;

And the same compilation error persists. Should it be?:

int[][][] arr = sReadFitsData(fname); // error is still there

When I'm putting this code with or without import it's ok:

int[][][] arr = ReadFitsData.sReadFitsData(fname); // working!

Solution

  • To be able to call methods from different classes (more specific: different files), you need to import those classes.

    The thing is, how should the class TestRead know who the method sReadFitsData is, at least it's no method from himself. Another thing is, what to do if TestRead would also define a method named sReadFitsData, you want somehow to be able to distinguish between both methods.

    There are two ways you can solve this issue. First, you can specify the full class path in front of the method call. You do so by exchanging the call in TestRead with: readFits.ReadFitsData.sReadFitsData(fname).
    The full class path consists of all packages and the class itself.

    Second, you can import the class, what you probably want to prefer in this case. Simply add this line to the beginning of the TestRead class, right after the package declaration: import readFits.ReadFitsData;
    It should then look like the other imports of your ReadFitsData class.

    Okay, that was the first problem. However, it won't compile now either. You must create an instance of the class ReadFitsData and call the method on it:

    ReadFitsData data = new ReadFitsData();
    data.sReadFitsData(fname);
    

    If you want the method to be independent of objects, you need to make it static:

    public static int[][][] sReadFitsData(String fitsFileName) throws ... {
    

    Then you can call the method by ReadFitsData.sReadFitsData(...) from anywhere (after importing).

    At this point I should say that you're struggling with some really basic Java things. It may be better for you to read or watch some beginner tutorials or books :)

    You may get an IDE, for example eclipse, for working with Java. Those programs will automatically import classes as they detect which method you probably want to use. Also they let you choose if there are multiple.