Ismail
Ismail

Reputation: 1316

Java : Which is better - returning an Object property or whole Object

I have a sequence of function calls like below

int empId = saveEmployee(emp);
int deptId = saveDepartment(dept, empId);
int areaId = saveArea(area, deptId);

1-My question which is better/ best practice in java to return the Object or its required properties,

Employee emp1 = saveEmployee(emp);
Department dept1 = saveDepartment(dept, emp1.getId());
Area area1 = saveArea(area, dept1.getId());

Assuming I only need the Id for my next method.

  1. And also, does using primitive types increase performance or memory consumption.

Upvotes: 1

Views: 81

Answers (2)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59960

  1. My question which is better/ best practice in java to return the Object or its required properties,

I would go with the default result of save method which return the Object.

<S extends T> S save(S entity);

and it still a choice for you in this point.

  1. And also, does using primitive types increase performance or memory consumption.

don't use primitive int instead use Long for ids, which can hold null

Upvotes: 2

Satyansh
Satyansh

Reputation: 31

As far as i think returning object would be better. As java is object oriented language, best practice is to follow proper access modifiers for each variables with proper getter setter method for each properties.

Upvotes: 0

Related Questions