views:

402

answers:

6
$ cat Const.java 
public class Const {
    String Const(String hello) {
        return hello; 
 }
 public static void main(String[] args) {
     System.out.println(new Const("Hello!"));
 }
}
$ javac Const.java 
Const.java:7: cannot find symbol
symbol  : constructor Const(java.lang.String)
location: class Const
  System.out.println(new Const("Hello!"));
                     ^
1 error
A: 

The constructor cannot return a value. That's final. It the same sense - it cannot have a return type and that's why you're getting the compile error. You may say that the return value is always implicitly the object created by the constructor.

Bozhidar Batsov
+5  A: 

Actually Constructor in a java class can't return a value it must be in the following form

public class Test {
 public Test(/*here the params*/) {
   //this is a constructor
   //just make some operations when you want to create an object of this class
 }

}

check these links http://leepoint.net/notes-java/oop/constructors/constructor.html http://java.sun.com/docs/books/tutorial/java/javaOO/constructors.html

Mohammad Kotb
A: 

A constructor can't have a return value like a "normal" function. It is called when an istance of the class in question is created. It is used to perform the initialization of that instance.

Venemo
+4  A: 

Constructors cannot return a value; they return the constructed object, so to speak.

You get an error because the compiler is looking for a constructor that takes a string as its argument. Since you did not declare a constructor the only constructor available is the default constructor that does not take any argument.

Why do I say you did not declare a constructor? Because as soon as you declare a return value/type for your method it is not a constructor anymore but a regular method.

From the Java Documentation:

A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that they use the name of the class and have no return type.

If you elaborate what you are trying to achieve someone might be able to tell you how you can get to that goal.

scherand
+4  A: 

What you've defined isn't actually a constructor, but a method called Const. If you changed your code to something like this, it would work:

Const c = new Const();
System.out.println( c.Const( "Hello!" ) );

If no specific constructor is explicitly defined, the compiler automatically creates a no-argument constructor.

Ash
A: 

Like already said, a constructor always returns the created object instance and nothing more.

But anyway you can more or less make a "custom constructor" in order to make it return something...

Exemple:

public class Const {

 public static String FakeConstructor(String hello) {
   new Const(hello);
   return "thestringyouwant";
 }

 public Const(String hello) {
    // blabla real constructor
 }
 public static void main(String[] args) {
     System.out.println(new Const("Hello!"));
 }

}

But this code has no interest since the created object instance won't be stored in any var (except if you specify it in FakeConstructor...) and the object will be garbage collected... you'd rather construct an object and then call an init method on it that could return something, it's be clearer for your teammates...

Sebastien Lorber