$ 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
问题:
回答1:
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.
回答2:
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.
回答3:
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
回答4:
A constructor can not return a value because a constructor implicitly returns the reference ID of an object, and since a constructor is also a method and a method can't return more than one values. So we say explicitely constructor does not have a return value.
回答5:
Many great answers already. I would just like to add that, if you want to get some return code separate from the object itself as a result of invoking a constructor, you can wrap the constructor in a factory method
which, upon creation, could for example do some data validation within the constructed object and return a boolean
depending on the outcome.
回答6:
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.
回答7:
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.
回答8:
I think the best way to produce the effect you want would be the following:
public class Const {
private String str;
public Const(String hello) {
str = hello;
}
public String toString() {
return str;
}
public static void main(String[] args) {
System.out.println(new Const("Hello!"));
}
}
This replaces the public String Const()
method you used previously, and by overriding the public String toString()
method of Object
(Which all Java classes inherit from) the String value of the object is printed correctly when you want to print the object, so your main method remains unchanged.
回答9:
public class Const {
private String myVar;
public Const(String s) {
myVar = s;
}
public String getMyString()
{
return myVar;
}
public static void main(String[] args) {
Const myConst = new Const("MyStringHere");
System.out.println(myConst.getMyString());
}
}
回答10:
To pass back a value from a constructor - just pass in an array as a parameter. To illustrate the principle:
Test() {
Boolean[] flag = new Boolean[1];
flag[0] = false;
new MyClass(flag);
System.out.println(flag[0]); // Will output 'true'
}
class MyClass {
MyClass(Boolean[] flag) {
// Do some work here, then set flag[0] to show the status
flag[0] = true;
}
}
回答11:
/************************************************
Put the return value as a private data member, which gets
set in the constructor. You will need a public getter to
retrieve the return value post construction
******************************************************/
class MyClass
{
private Boolean boolReturnVal;
public Boolean GetReturnVal() { return(boolReturnVal); }
MyClass() // constructor with possibly args
{
//sets the return value boolReturnVal
}
public static void main(String args[])
{
MyClass myClass = new MyClass();
if (myClass.GetReturnVal())
{
//success
}
}
}