Rakesh KR
Rakesh KR

Reputation: 6527

Class Variables Vs Method Variables

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?.

Upvotes: 2

Views: 2428

Answers (4)

David George
David George

Reputation: 3752

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.

Upvotes: 2

Anil Reddy Yarragonda
Anil Reddy Yarragonda

Reputation: 767

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.

Upvotes: 1

Phil Anderson
Phil Anderson

Reputation: 3146

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.

Upvotes: 2

Dragondraikk
Dragondraikk

Reputation: 1679

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.

Upvotes: 7

Related Questions