2

While i am defining a class, i prefer to use following format.

public class ClassName{

   private String             var1 = null;
   private Map<String,String> map1 = null;

   public void process(){

      try{
           var1 = "Variable1";
           map1 = new HashMap<String,String>();

            // Do Some Stuffs Here with the varaibles.

      } catch(Exception e){
           e.printStackTrace();
      } finally{
           var1 = null;
           map1 = null;
      }

   }

}

But my friends suggest me to use following way,

public class ClassName{

   public void process(){

      String             var1 = null;
      Map<String,String> map1 = null;

      try{
           var1 = "Variable1";
           map1 = new HashMap<String,String>();

            // Do Some Stuffs Here with the varaibles.

      } catch(Exception e){
           e.printStackTrace();
      } finally{

      }

   }

}

My question is which is better and why?.

7
  • possible duplicate stackoverflow.com/questions/5539652/… Commented May 27, 2015 at 15:02
  • There is no better way. It depends on the purpose. Ask yourself, are these variables part of my class? Commented May 27, 2015 at 15:02
  • to add to @azurefrog comment, here is some great reasons why private is better to use programmers.stackexchange.com/q/143736 Commented May 27, 2015 at 15:03
  • 2
    That doesn’t change anything. Why do you want to keep variables outside the method’s execution time, just holding null references? If you need variables inside a method, declare them inside the method. Commented May 27, 2015 at 15:06
  • 3
    @RakeshKR It's a good rule of thumb to always scope variables as tightly as possible. It's a habit which will save you hours of debugging later in your career. Commented May 27, 2015 at 15:07

4 Answers 4

7

This depends entirely on the situation. It is generally a good idea to define variables with the smallest scope possible. So unless you plan on using your variables outside of the method, just make them method variables.

Sign up to request clarification or add additional context in comments.

Comments

2

Your friend's suggestions is better if the variables are only used by the process() method. You should generally always use the smallest scope you can.

By making them propertes of the class (as you did) you are using unnecessary space on the heap, and also making the class non-threadsafe without getting any benefit from doing so.

Comments

2

Your class level variable will exist for the lifetime of the class. If you need to access the variable via a getter/setter or maybe different methods need to access the same data than make it class level. Otherwise make it method level. Method level or local variables are declared on the method's stack and can be primitive types or references to objects. They come into existence or scope when the method is called and cease to exist when the method exits. The GC isn't involved in this although the objects they reference may be GCed.

It is easier to test classes that only use method level variables.

Remember a class level variable is effectively a "global" variable (at class level if private) with all the caveats that go with it. Different methods can access the variable and it may be difficult to work out which piece of code is setting the value.

Comments

1

Your friend code is good. If you are making local variables those variables are loading into the memory along with your method and scope also method scope (Smallest Scope). If you are created Global variables those need to store into the memory separately.

if you want to use only in one method then its better to create local variables rather than going with class variables.

2 Comments

Although you are right somehow, I don't think that answers the question.
You are not discussing, you're provding an answer. Maybe add a comment instead. No offense.

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.