java - Why doesn't assigning two concatenated strings to another string work? -


i experimenting string class in java , wanted override tostring() method when came across issue. did -

public class string1 {  private string s = "hello world";  public static void main(string[] args) {      string1 s1 = new string1();     system.out.println(s1.s.tostring()); }  public string tostring() {     string1 s2 = new string1();     s2.s = this.s + " lel";     return s2.s; }  } 

output -

hello world.

how modify tostring() method outputs

hello world lel

use s1.tostring() instead of s1.s.tostring()

s instance of string

s1 instance of string1 [your class]

you have defined tostring() string1.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -