0

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.

Darshan Patel
  • 3,176
  • 6
  • 26
  • 49

3 Answers3

3

You are creating a new object inside your method, so your old object is not being updated, modify your code:

public void test1() {
    TestObject to = new TestObject();
    to = test2(to); // THIS line, to receive the object you created in test2()
    to.printValues();

    TestObject to2 = null;
    to2 = test2(to2);
    to2.printValues();
}
morgano
  • 17,210
  • 10
  • 45
  • 56
  • yes,i also use this solution, but i passed Object and in java everything is pass by reference i also tried by passing new Object rather than null and just changed condition to `_to != null` but also values are not set in Object – Darshan Patel May 07 '14 at 05:26
  • you're confusing variable and object: yes, you're passing a reference to an object (your var `_to`) but as you are updating `_to` to a **new** reference to another object (using `new TestObject()`) then you lose the reference to the former object – morgano May 07 '14 at 05:30
  • Yup I got this but let me confirm if i am creating new Object in function and return back to caller function then i have to reassign that object in caller function. is this correct? – Darshan Patel May 07 '14 at 05:33
  • correct, you need to return back the new reference to the new object (the updated `_to`) to the calling function – morgano May 07 '14 at 05:35
0

In your test2() function even if the object is not null you create a new object. Instead you can populate the fields of the existing object. Just change the following piece of code :

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();  - Not needed as object is not null you can populate existing object
        _to.setId("Some ID");
        _to.setName("Some Name : "+new java.util.Date());
    }
    return _to;
}
Kakarot
  • 4,252
  • 2
  • 16
  • 18
0

Java is pass by value. If you modify any an object which you got from method signature will not affect the original object unless the original object is assigned with modified object.

Update your code as below:

TestObject to = new TestObject();
to = test2(to); 
to.printValues();

See This

Community
  • 1
  • 1
G.S
  • 10,413
  • 7
  • 36
  • 52