Winston Gutkowski wrote:
Monica. Shiralkar wrote:if it is needed only for small operation keep it method variable. Is that right?
Actually, you can go further and say "if it is only needed by one method, make it a method variable".
@OP: This is a useful guideline, but you need to be aware that the converse is not necessarily true. That is, just because it's used by multiple methods, that doesn't mean it should be a member. If several different methods can perform different operations on the same data, but each one uses it only in a self-contained fashion, it should be a local. Likewise, if method X() calls Y() which calls Z() and all 3 use that variable, that doesn't necessarily mean it should be a member.
The main attribute for a variable to be a member variable is that it is part of the object's state. That is, that variable keeps its value even when no methods are running, so that at any time we can look at that object and say, "This X's Y is Z," such as "This Person's name is 'George'".