I know this is a little dumb, but how do you do this? I've "RTFM" but I still don't understand concepts like this just don't exist in the languages I'm used to programming. Anyway, my question is simple: how does one properly set a global variable which can be used by all public void functions inside that class?
Here is some example code, I will highlight the redundancy in case you don't see it:
public class baketimer extends Activity implements OnClickListener {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button cupcake1 = (Button) this.findViewById(R.id.cupcake1);
cupcake1.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.cupcake1:
final Button cupcake1 = (Button) this.findViewById(R.id.cupcake1);
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
cupcake1.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
cupcake1.setText("Done!");
}
}.start();
To开发者_StackOverflowast.makeText(this, "Mmm cupcakes!", Toast.LENGTH_SHORT).show();
break;
How would I go about declaring cupcake1
for the entire class?
Thanks in advance!
Declare the variable inside the class and outside any function, but not necessarily static.
public class YourClass{
Button cupcake = null;
public void onCreate(Bundle savedInstanceState) {
...
cupcake = (Button) this.findViewById(R.id.cupcake1);
...
}
public void onClick(Bundle savedInstanceState) {
...
}
}
Use static (static Button cupcake = ...
) if you want the variable to be shared by ALL objects - aka ALL instances of the class - in your program. Otherwise, don't use static, so that the variable only belongs to that object.
For the entire class, you define it as static
inside the class but outside any function, such as:
public class testprog {
static int xyz = 0;
public static void main(String args[]) {
}
}
But make sure that's what you want. That one single variable will be shared amongst all instances of your class and you'll probably have to synchronise access to it if you're using threads. If you want something that's available to all functions in your class but still has one per instance, leave off the static.
First we need to be clear what you mean by "global variable".
- Java has
static
variables that are shared by all instances of a class. - Java has instance variables that belong to a single instance of a class.
- Java has local variables and parameters that exist while some method (or code block) is executing.
- Java does not have global variables in the sense of C/C++.
Having said that, here's a simple example that declares a static and an instance variable.
public Foo {
private static int fooCounter;
private int nosLegs;
public Foo (int n) {
nosLegs = n;
fooCounter++;
}
/* There is only one counter of Foo instances created */
public static int getFooCount() {
return fooCounter;
}
/* Each Foo can have a different number of legs */
public int getNosLegs() {
return nosLegs;
}
}
精彩评论