ray
ray

Reputation: 4250

Remove key and value from object

My data model is as follow:

public class CustomerObject implements Serializable
{
public Integer pkid;
public String code;
public String name;

    public CustomerObject()
{
    pkid = new Integer(0);
    code = "";
            name = "";
    }
}

Now I am calling this from another class:

public CustomerObject getCustObj() {
    CustomerObject custObj = new CustomerObject();
    custObj.pkid = new Integer(1001);
    custObj.code = "CUST1001";

    return custObj;
}

Now here in getCustObj() function I want to pass only pkid and code. I mean I want to remove the variable "name" from the object and then pass. So my passing object will look like:

    CustomerObject()
    {
        pkid = 1000;
        code = CUST1001;
    }

Please help how I can do this.

Actually I have a data model of 200 variable. I will pass this using webservice. But during pass by webservice I may need only 20 to pass. So I want to reduce the data size.

Upvotes: 0

Views: 581

Answers (2)

Duminda Jayarathne
Duminda Jayarathne

Reputation: 390

Use another constructor in class CustomerObject as following.

public class CustomerObject implements Serializable
{
    public Integer pkid;
    public String code;
    public String name;

    public CustomerObject()
    {
        pkid = new Integer(0);
        code = "";
        name = "";
    }
    public CustomerObject(int inPkid, String inCode)
    {
        this.pkid = inPkid;
        code = inCode; 
    }
}

When you call getCustomerObject method from another class use as follows

public CustomerObject getCustObj() {
    CustomerObject custObj = new CustomerObject(new Integer(1001),"CUST1001");
}

Upvotes: 1

Popeye
Popeye

Reputation: 1558

If you are not setting name in your object, then it is as good as object not having name because name is null. You can't remove name variable from the object. But if you really want to do so, you can use inheritance. Make one class with all attributes except name and other class extends the first class and adds name attribute to it. So now you can use first class when you don't need the name attribute.

Upvotes: 0

Related Questions