views:

435

answers:

11

Hi all,

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.

A: 

No. A constructor does not really return anything. Instead it builds the object in question.

It looks like you want a utility method here:

public class Whatever {
   public static String doStuff(String s) {
      return s;
   }
}
mlk
+12  A: 

Constructors create objects, they don't return data.

pave
A constructor is a method with no return type specified (not even void) and with an identifier that is the same as the class name. If you're returning anything at all, you're not writing a constructor. Constructors are called with the `new` keyword, so if you're trying to use an existing constructor without using `new`, you're doing it wrong. HTH.
Matt Ball
That's a good point, you CANNOT say that It actually returns an instance of the new object created. Simple answer, good for your reputation :-)
LB
If you want to get pedantic about it, the constructor does not return anything, but it does evaluate to a reference when assigned to a variable. :P
Matt Ball
I thought the answer should be simple. It's more about the concept than the execution.
pave
I agree with pave. Simple is good. elwynn needed a quick and simple *"this is why"*, not an in depth academic discussion.
Stu Thompson
+2  A: 

Constructor is not a regular method. It always returns instance of the class that it belongs to. In your example ArionFileExtractor. There is no way to return any other instance.

Notice that you can't specify return type for constructor explicitly nor use return keyword (illegal in this context).

pregzt
a Constructor does not return anything, it's the new expression (Class Instance Creation Expression) that returns the instance. ((also in byte code the constructor returns void))
Carlos Heuberger
+1  A: 

Constructor can only return instance of its class. It cannot return String. For instance if you have class SampleClass, constructor can return only object of class SampleClass.

pir0
A constructor does not return anything. It initializes data.
Matthew Sowders
+5  A: 

A constructor can only return an instance of object that it constructed - otherwise you have no reference against which to hang on to the object you just created! If you want to make a "utility" call, consider a static method:

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

Which can be invoked using

ArionFileExtractor.getFileContents(...)
butterchicken
+1 for the hint to a static method.
elwynn
Replace the :: to a . and you're all set, since it's Java. :)
crunchdog
Actually `ArionFileExtractor.getFileContents(...)` in Java (which doesn't use :: for anything). But +1 anyway.
David Zaslavsky
@crunchdog, @David GAH! Thanks for the spot, have taken my C++ head off for the rest of the afternoon.
butterchicken
+8  A: 

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.

Stu Thompson
+1 for the code!
elwynn
+1  A: 

No it should not be able to legally return a String. I'm not sure why Netbeans didn't simply barf at you when you tried to. Maybe it tried to compile it as some sort of static method. Constructors do not generally have return types in code, because when they are compiled they are assigned to return an instance of the class they are constructing.

The easiest (though not necessarily best) way to adapt your code would be to have an empty constructor and turn the current constructor into a static method like this:

public class ArionFileExtractor {

private ArionFileExtractor() {}

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

The private constructor makes it so that ArionFileExtractor can only be used statically and cannot be instantiated. Then when you use it you simply do this:

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

Be warned, using static classes is sometimes considered bad form - depending on the situation. So it might be worth while to try and come up with a different way to do this.

Daniel Bingham
+1 for extremely useful comment.
elwynn
+2  A: 

Java compiler treats ArionFileExtractor as an instance method,

String afe = new ArionFileExtractor().ArionFileExtractor("gibberish.txt", "foo", "/foo");
adatapost
+4  A: 

As much as this is surprising, the code you made has a default no argument constructor. ArionFileExtractor is a method that returns a String. I was quite surprised when I first saw code that did this, as it is certainly an accident (as in your case).

You could call you method (just to show this is the case) with:

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

What it really sounds like you are trying to get at is a static method, not a class at all.

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

which you would call with:

String afe = ArionFileExtractor.extract("gibberish.txt", "foo", "/foo");
Yishai
+1 for raising the default constructor issue!
elwynn
A: 

Yes.Only String class constructor can return string objects out of it ;).

This basically means that the constructor creates the object of the class you are calling.

A: 

You need to FIRST create the object (using the constructor) and THEN do stuff with it.

Java has very few smart shortcuts. I

Thorbjørn Ravn Andersen