2

I have a question about local and member variables in Java. The situation is: Sometimes if I define a local variable, that variable has to be passed around into several levels of method calls. I often think, why should I just define a member variable for the class so that the variable is available everywhere in the class. A member variable of a class is like a global variable accessible everywhere in the class.

  1. What's the best practice for this scenario? OR what's the right thing to do?

  2. If I define it as a member variable, should it be a static or non-static variable?

tiago
  • 22,602
  • 12
  • 72
  • 88
user697911
  • 10,043
  • 25
  • 95
  • 169

6 Answers6

5

Member variables hold characteristics -- attributes is another term -- for the 'thing' in your program represented by the class. If your variable holds something like that, make it a member variable; if it doesn't, don't.

Static variables hold characteristics of the class itself, as opposed to characteristics of objects of that class.

Don't make the decision based on whether you "pass ... into several levels of method calls.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • To add to this...remember that you can pass arguments from one method to another method in the same class. On the other hand, if a lot of methods need to access the same variable, then chances are it should be a member variable. – Matt Browne Apr 12 '13 at 21:14
  • 1
    I agree the chances are that it should be, but I think this is looking at it from the wrong direction. If you design your classes by how their methods behave, you're doing it bottom-up, and *trying* to look at the forest by examining individual trees. What the member variables are should be decided before you have *written* any methods; if you have something that is getting passed around "a lot", then look at the design overall; perhaps there is something else that should be a class and isn't. And remember there is nothing wrong, by itself, with passing a variable through a lot of methods. – arcy Apr 12 '13 at 23:37
  • I agree with you; I probably should have said, "you should re-evaluate your design to see if it should be a member variable" instead of "chances are it should be a member variable." – Matt Browne Apr 16 '13 at 16:00
2

Thinking about your general question, I came up with these guidelines from my experience:

  • Use the smallest poosible scope. If a local variable is fine, then why use a member variable?
  • Model the class according to the domain. If you have a value and it is part of the class responsibility, then modeling it as a class member seems to be appropiate.

To your second question: I do generally define static variables as final which make them constants. Non-final static members in a multi-threaded environment may easily introduce race conditions. It is easier to make access to non-static member thread-safe if needed.

remipod
  • 11,269
  • 1
  • 22
  • 25
  • Regarding static variable as 'final', but often that variable has to change. For instance, you have an ArrayList whose size keeps changing when some methods are called. So, final should be used in limit. For instance, you have a static initialization of that variable then you know it won't change during the run of the program. Right? – user697911 Apr 12 '13 at 21:25
  • Declare an ArrayList as a final static member and objects can still be added or removed from the ArrayList. A final static member cannot be reassigned => the reference to the ArrayList cannot be modified. Understandable? – remipod Apr 13 '13 at 08:06
  • Here is a good explanation: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html If you want the static final ArrayList to be unmodifiable, check this method http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#unmodifiableList(java.util.List) – remipod Apr 13 '13 at 08:11
0
  1. If you decide to declare a member variable then you should consider the variables scope. If you are just reducing the number of parameters on internal method calls I would consider declaring the variable as private.

  2. You should be careful of using Static because every instance of the class will use the same copy of the variable.

Rylander
  • 19,449
  • 25
  • 93
  • 144
0

It's behavioral. If your variable is part of your class attribute i.e. characterizes the class behavior then define it as the class member variable otherwise define a local variable inside the method.

In my opinion, passing the argument doesn't contribute in the attribute type definition.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
0

It depends.

If you want all (or several), of your methods to access the same member variable, then declare it in the class.

Static or not depends on wether that information belongs to the Class (static), or to an instance of the class (non-static).

pcalcao
  • 15,789
  • 1
  • 44
  • 64
0

1.) If you are going to use it in multiple methods and need to pass it around make its member variable. If it is just for that method make it local.

2.) static means their is only one instance of that variable that is shared among everything. So for example I have a totalGameScore variable that will keep my score for the whole game no matter what. You'd want to make that variable static. Other than that lets say I have a health variable for my enemy. Each enemy will have its own health so I would NOT make that variable static.

You can also declare member variables private so that other classes cannot access them. Constants should normally always be member variables.

Joban
  • 1,318
  • 7
  • 19