0

I have a Bean class that calls 2 seperate DAO's to pull info from DB. This is the structure

class InfoRetriever {

public String retrieveInfo(int arg1, int arg2){
      String info = retrieveFirstInfo(arg1 , arg2);

      if(info.equals("xyz")){
          retrieveSecondInfo(arg1, arg2);
      }
}

private String retrieveFirstInfo(int arg1,String arg2){
     // call DB to get info
}

private String retrieveSecondInfo (int arg1, String arg2) {
    // call DB to get info
}

}

My questions is that i have a choice to move arg1 and arg2 as member elements and can set them before calling retrieveFirstInfo and retrieveSecondInfo . I can also make info as member variable.

What are the trade offs to be considered if there is a choice to keep a variable local to the method vs class variable.

3
  • 2
    More than trade-off, you should consider whether those arguments should really be made members of the class. Do they belong there? Are they supposed to maintain states of the objects? If yes, then yes, definitely make them fields. Commented Jan 29, 2014 at 15:58
  • @rohit - That;s what my questions is.Now this is a somewhat service / manager class. these are not entity / real world objects and hence naking it diffcult for me to identify abstraction. Commented Jan 29, 2014 at 16:00
  • Then I would leave it as it is. There is nothing wrong in passing them as arguments, if they don't fit good as member variables. Also, your retrieveInfo method would also be receiving those arguments from the caller right? Check the syntax there. You are missing (). Commented Jan 29, 2014 at 16:02

1 Answer 1

1

If any value of arg1 and arg2 give you the same info, then, the right way to :

  • Make info member variable
  • declare a constructor that takes arg1 and arg2 and arguments
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Amit. I am going to make info as member element as i can even pull this intermediatery info using a getter if required in future. Though i don;t find building any info using constructor right in my case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.