Java: How can a constructor return a value?

2019-01-18 03:50发布

$ 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

11条回答
女痞
2楼-- · 2019-01-18 04:10

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楼-- · 2019-01-18 04:11
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());
  }
}
查看更多
来,给爷笑一个
4楼-- · 2019-01-18 04:13

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.

查看更多
不美不萌又怎样
5楼-- · 2019-01-18 04:16

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.

查看更多
我只想做你的唯一
6楼-- · 2019-01-18 04:19

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

查看更多
女痞
7楼-- · 2019-01-18 04:20

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.

查看更多
登录 后发表回答