tags:

views:

96

answers:

3

Hi everyone, yep, this is a total newb question, 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();
            Toast.makeText(this, "Mmm cupcakes!", Toast.LENGTH_SHORT).show();
            break;

How would I go about declaring cupcake1 for the entire class? I know, this must be so newb, why I can't find it, I dunno.

Thanks in advance!

A: 

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.

paxdiablo
Thank you very much for the insight
AutoM8R
I wanted to upvote, but I am too new to the site :(
AutoM8R
There ya go, @AutoM8R, here's a question upvote to take you to 15 rep, now you can upvote to your heart's content :-)
paxdiablo
A: 

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 in your program. Otherwise, don't use static, so that the variable only belongs to that object.

Louis Rhys
Hmm and that was exactly the OBVIOUS thing to do AND I did try that. Now I am boggled, because when I do this it creates an error. I must be missing a semi-colon, doh!@both of the answers, thanks for helping on such a newb question!
AutoM8R
ok. I won't mind if you upvote or accept this too :D
Louis Rhys
Thank you, could you please explain how to set the button such as the code required to complete the above example?I regularly use findViewById, but I suspect because setContentView isn't called until onCreate() I can't declare the button using findViewById without it throwing a force close, am I right?Cheers!!
AutoM8R
I am also new to StackOverflow, so please bare with me, I'm trying I promise :)
AutoM8R
edited my answer. will that do for you?Basically cupcake will be null when you first initialise the class, and will be set at onCreate
Louis Rhys
Oh of course, haha!
AutoM8R
*"... so please bare with me"*. It's a good thing your PC's video camera is turned off! :-) :-)
Stephen C
Get rid of the `this` in front of the method name. It just clutters up the code.
Steve Kuo
A: 

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;
   }
}
Stephen C
Awesome example, thanks for all the support!
AutoM8R
I really am not sure who has the best answer, they both portray great angles! I would love to vote this up, but I do not have the required rep yet.
AutoM8R