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.