I have two classes as follows package com.test;
public class TestObject {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void printValues() {
System.out.println("ID = "+id);
System.out.println("Name = "+name);
}
}
and
package com.test;
public class Test {
public void test1() {
TestObject to = new TestObject();
test2(to);
to.printValues();
TestObject to2 = null;
to2 = test2(to2);
to2.printValues();
}
public TestObject test2(TestObject _to) {
if(_to == null) {
_to = new TestObject();
_to.setId("System ID");
_to.setName("System Name : "+new java.util.Date());
} else {
_to = new TestObject();
_to.setId("Some ID");
_to.setName("Some Name : "+new java.util.Date());
}
return _to;
}
public static void main(String[] args) {
Test test = new Test();
test.test1();
}
}
when i run this code i got following output
ID = null
Name = null
ID = System ID
Name = System Name : Wed May 07 10:38:22 IST 2014
I don't if object is null then i am creating from function and returned from function so why it gives me null?
please help me
Thanks in advance.