Vivek Vermani
Vivek Vermani

Reputation: 2004

Class variables vs local variables

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.

Upvotes: 0

Views: 112

Answers (1)

Amit Sharma
Amit Sharma

Reputation: 6174

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

Upvotes: 1

Related Questions