Nirojan Selvanathan
Nirojan Selvanathan

Reputation: 11164

Assign variable inside a method

All objetcs (p1, p2 buyer, seller) are from the same class and initially buyer and seller objects are null. Based on the type of the document I need assign p1 and p2 to either seller or buyer.

Class EmailSenderUtil{
public void sendPendingActionEmail(Document Type, PartyType p1, PartyType p2){
  PartyType buyer = null;
  PartyType seller = null;
  /// some other computations

  else if(documentType.equals(DocumentType.REQUESTFORQUOTATION)) {
      assignInitiatorAsBuyer(p1, p2, buyer, seller);
     // set some other variable unique to this type
  }
}

private void assignInitiatorAsBuyer(PartyType p1, PartyType p2, PartyType buyer, PartyType seller) {
        buyer = p1;
        seller = p2;
    }
private void assignInitiatorAsSeller(PartyType p1, PartyType p2, PartyType buyer, PartyType seller) {
        buyer = p2;
        seller = p1;
    }
}

I can assign these variables in 2 lines but as the else if conditions are alot(10) inorder to reduce the redundancy I moved this assignment inside a method. But as Java is pass by value the assignments are not reflected to the parent method.

Would be much obliged If I could know is there any elegant way to perform this operation rather than copying the same assignment several times.

Upvotes: 0

Views: 123

Answers (2)

Yashi Srivastava
Yashi Srivastava

Reputation: 187

After the edits provided in the question, i think we can now go with the approach where we can remove buyer and seller from the method, and use this.buyer=p1

Class EmailSenderUtil{
   PartyType buyer = null;
   PartyType seller = null;
    public void sendPendingActionEmail(Document Type, PartyType p1, PartyType p2){
 // .....
 // rest of the code

else if(documentType.equals(DocumentType.REQUESTFORQUOTATION)) {
    assignInitiatorAsBuyer(p1, p2, buyer, seller);
  }
}

private void assignInitiatorAsBuyer(PartyType p1, PartyType p2) {
    this.buyer = p1;
    this.seller = p2;
}
private void assignInitiatorAsSeller(PartyType p1, PartyType p2) {
    this.buyer = p2;
    this.seller = p1;
}

Upvotes: 2

Ali Azim
Ali Azim

Reputation: 180

If the methods are already present in PartyType use these:

else if(documentType.equals(DocumentType.REQUESTFORQUOTATION)) {
      assignInitiatorAsBuyer();
 // set some other variable unique to this type
}

private void assignInitiatorAsBuyer() {
        this.buyer = this.p1;
        this.seller = this.p2;
}
private void assignInitiatorAsSeller() {
        this.buyer = this.p2;
        this.seller = this.p1;
}

Or if it's used in other class use like this:

else if(documentType.equals(DocumentType.REQUESTFORQUOTATION)) {
      assignInitiatorAsBuyer(partyTypeObj);
 // set some other variable unique to this type
}

private void assignInitiatorAsBuyer(PartyTypeObjects partyTypeObj) {
        partyTypeObj.buyer = partyTypeObj.p1;
        partyTypeObj.seller = partyTypeObj.p2;
}
private void assignInitiatorAsSeller(PartyTypeObjects partyTypeObj) {
        partyTypeObj.buyer = partyTypeObj.p2;
        partyTypeObj.seller = partyTypeObj.p1;
}

Upvotes: 0

Related Questions