Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I declare my variables?

When declaring my variables at the beginning of the class, I can't use them in a conditional statement in other methods. I tried declaring them inside the method it works but I can't use the variables outside the method.

This is a sample of my code

protected void onClick(View v){
  switch(v.getId()){        
    int counter;
    case R.id.btsend:
      for (int i = 0;i<=0;i++){          
        if (true)
          counter += 1;          
      }

    TextView.setText(counter);

    break;
  }
}
like image 932
user3481961 Avatar asked Apr 11 '14 08:04

user3481961


2 Answers

There's no "one size fits all" answer here because there are different reasons for declaring variables.

Static variables (declared directly within the class, with the static modifier) are class-wide, rather than specific to a single instance. Usually static variables are final - mutable global state is a tricky business.

Instance variables (declared directly within the class) are there to record the state of the object. They will be available within every instance method.

(Instance and static variables are both fields.)

Local variables (declared within a constructor or method) are only available within that method, and are effectively "temporary" data which is only required for that single method invocation. (If the method is called recursively, each call gets its own independent set of variables.)

So for each variable, consider whether it's logically part of the state of the object or not, and declare it in the appropriate place. Once you've decided what kind of variable it is, you can then determine the physical placement. For fields, it doesn't matter (much) exactly where you declare it - at the top of the file, in the middle, or at the bottom. The ordering between fields matters in terms of initialization, but it doesn't matter whether it's before or after a method that uses it. It's generally a good idea to declare all your fields in one consistent place though - personally I usually have:

class Foo
{
    // Static variables
    // Instance variables
    // Constructors
    // Methods
    // Nested types
}

... but that's just a convention.

For local variables, it's generally a good idea to declare them as late as possible, within as small a scope as possible. So instead of:

int i;
int x;
for (i = 0; i < 10; i++) {
    x = i * i;
    System.out.println(x);
}

This would be preferrable:

for (int i = 0; i < 10; i++) {
    int x = i * i;
    System.out.println(x);
}

By limiting the scope of a variable, it's typically easier to reason about it.

like image 125
Jon Skeet Avatar answered Sep 27 '22 21:09

Jon Skeet


You need to declare variables

  • in a place where declarations are allowed by the Java syntax, and

  • in a place that is appropriate to the intended lifetime of ... whatever it is.

So, in your example, you can't declare counter at that point, because the syntax of a switch statement does not allow it. But you could put it before the switch statement. Or before the method, or after it.

Next you need to ask yourself how long counter is supposed to "live". If it is supposed only to live while onClick is running, then it needs to be declared inside the onClick method. But if it is intended to live as long as the instance of the object ... then it should be an instance variable ... and declared at the top of the class.

And so on ...

In short, the correct answer depends on what you are trying to do ...


(In this case, if counter is supposed to count the number of clicks on ... "this thing" ... it probably should be an instance variable. It could also be a static variable, modulo the issue that that is likely to be a poor design choice, and it could be incorrect for other reasons. A local variable would definitely be incorrect, as a click counter.)

like image 40
Stephen C Avatar answered Sep 27 '22 20:09

Stephen C