1

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

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
  • possible duplicate http://stackoverflow.com/questions/5539652/using-a-class-variable-vs-sending-local-variable-to-functions-methods – Bacteria May 27 '15 at 15:02
  • There is no better way. It depends on the purpose. Ask yourself, are these variables part of my class? – JacksOnF1re May 27 '15 at 15:02
  • to add to @azurefrog comment, here is some great reasons why private is better to use http://programmers.stackexchange.com/q/143736 – stackoverfloweth May 27 '15 at 15:03
  • I would even move the declaration into the `try` block and remove the initial `null` values: `try { String var1 = "Variable1"; Map map1=new HashMap(); …`. Generally, if your code contains something like `finally{ var1 = null; map1 = null; }` you should reconsider your design. – Holger May 27 '15 at 15:04
  • If only one method is present in the class, Which one is better? – Rakesh KR May 27 '15 at 15:06
  • 1
    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. – Holger May 27 '15 at 15:06
  • 2
    @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. – azurefrog May 27 '15 at 15:07

4 Answers4

5

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.

Dragondraikk
  • 1,659
  • 11
  • 21
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.

Phil Anderson
  • 3,146
  • 13
  • 24
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.

David George
  • 3,693
  • 1
  • 18
  • 22
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.