Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can constructors actually return Strings?

I have a class called ArionFileExtractor in a .java file of the same name.

public class ArionFileExtractor {

public String ArionFileExtractor (String fName, String startText, String endText) {
    String afExtract = "";
    // Extract string from fName into afExtract in code I won't show here
    return afExtract;
}

However, when I try to invoke ArionFileExtractor in another .java file, as follows:

String afe = ArionFileExtractor("gibberish.txt", "foo", "/foo");

NetBeans informs me that there are incompatible types and that java.lang.String is required. But I coded ArionFileExtractor to return the standard string type, which is java.lang.string.

I am wondering, can my ArionFileExtractor constructor legally return a String?

I very much appreciate any tips or pointers on what I'm doing wrong here.

like image 409
elwynn Avatar asked Nov 28 '22 00:11

elwynn


2 Answers

Constructors create objects, they don't return data.

like image 183
pave Avatar answered Jan 05 '23 07:01

pave


Your method, ArionFileExtractor(), is not a constructor. Consutructors do not have return types, and look like this:

public  ArionFileExtractor (String fName, String startText, String endText) {
    //...
}

Note the lack of a return type.

like image 25
Stu Thompson Avatar answered Jan 05 '23 09:01

Stu Thompson