Why does the this
keyword print exactly what the toString
method is printing?
public class A {
public static void main(String[] args) {
Stuff s = new Stuff("in",5 );
System.out.println(s);
double doubleValue = 2.5 ;
s.doSomething(doubleValue);
System.out.println(doubleValue);
s = new Stuff("more", 3);
String str = "word";
System.out.println(s.changeSomething(str));
System.out.println(s);
System.out.println(str);
}
}
class Stuff {
private static final int n =2 ;
private String string;
private int num;
public Stuff(String s, int num) {
this.num = num;
string = s;
}
public void doSomething(double d) {
d = d * num;
System.out.println(this);
}
public double changeSomething(String s) {
s = string;
return n * num;
}
public String toString() {
return string + " has " + num;
}
}
This is the output I get
in has 5
in has 5
2.5
6.0
more has 3
word