Witam jestem początkujący mam takie pytanie w jednej z ksiązek znalazlem taki kod
package newpackage;
/**
*
* @author luppo
*/
class Simple
{
String s;
public Simple (String si)
{
s= si;
}
public void setString (String sNew)
{
s = sNew;
}
}
class Second
{
Simple simple;
String s;
public Second(String si)
{
s = si;
}
public void check()
{
if( simple ==null)
{
System.out.println("simple is null not initialized");
}
else
System.out.println("simple isn't null initialized");
}
private Simple lazy()
{
if (simple == null)
{
System.out.println("created Simple");
simple = new Simple(s);
}
return simple;
}
public Simple getSimple()
{
return lazy();
}
public String toString()
{
return lazy().toString();
}
public void setSimple(String sNew)
{
lazy().setString(sNew);
}
}
public class S212 {
public static void main(String[] args){
Second second = new Second ("Init String");
second.check();
System.out.println(second.getSimple());
second.check();
System.out.println(second);
second.setSimple("new string");
System.out.println(second);
}
}
Program się kompiluje tylko taki jest wynik
simple is null not initialized
created Simple
newpackage.Simple@10385c1
simple isn't null initialized
newpackage.Simple@10385c1
newpackage.Simple@10385c1
Chcialem zapytac dlaczego jest zwracana referencja "newpackage.Simple@10385c1" a nie nazwa" Init String "??
Z góry dziękuje za pomoc