0

I defined a class (as a substitute to macros in c)

public class Constants {

    public static final int i1 = 1;
    public static final int i2 = 2; 
    }

And another "global variable" class

public class GlobalVars {
    public static Integer gi1;
    public static Integer gi2;
}

I assign like this:

GlobalsVars.gi1 = Constants.i1;

While I do not get any compiler warning and it works in 1000 test cases, is it possible that this causes GlobalVars.gi1 to become null in special cases - like on certain Android devices running various versions?

EDIT:

I compare like this:

if (GlobalVars.gi1 == Constants.i1)

and this is where the NullPointerException error occured

user387184
  • 10,953
  • 12
  • 77
  • 147

2 Answers2

2

This will work fine in all cases. It uses a feature of the Java language called autoboxing to do the conversion from the Integer object to an int primitive. http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html

It doesn't matter which version of Android you run on.

Damian
  • 8,062
  • 4
  • 42
  • 43
  • Thanks for the quick reply, I am just asking because I got a crash report of my app in a place where it compares if (GlobalVars.gi1 == Constants.i1) - is that also taken care of with autoboxing? – user387184 Nov 04 '11 at 11:07
  • It should auto convert for you as described here: http://stackoverflow.com/questions/7672317/integer-int-allowed-in-java . You mention a null pointer exception - make sure the Integer value is not null! – Damian Nov 04 '11 at 11:17
  • thanks - but the integer value cannot be null because I declare it in the Class Constants as shown above. Is this right? Or can it be null in certain cases anyway (like Janusz decribed). (I never change this value anywhere) – user387184 Nov 04 '11 at 11:20
  • But the Integer variable is null. Like Janusz described: You see in [the Activity lifecycle](http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle) that after the `onPause()` function the process can be killed (I don't know where you set the GlobalVars, but possibly they won't be rebuilt and stay `null`) -- It has nothing to do whether `Integer gi = i;` or `Integer gi = new Integer(i);` is used (as mentioned before: It is a sporadic error - if the declaration would cause the problem, the error occurence would be systematically) – Philipp Wendt Nov 04 '11 at 11:30
2

The code SHOULD run fine on all current devices.

There is at least one problem with this:

Using static global variables is not recommended on Android. There is no specification how static class variables are treated.

That means it is possible that at some point if the device needs very much memory your whole app is removed from memory if the app is then brought back to the foreground all the activities will be rebuild from a saved instance state but know you can't rely on your static variables to be still available. This is not a problem with int and Integer. It is a problem with the persistence of static variables if your App is removed from memory and all your classes are load again once the app gets recreated.

Janusz
  • 187,060
  • 113
  • 301
  • 369
  • so does that mean that static values are not rebuild in these cases while non-static variables will be rebuild. Should I then just declare them public int i1 = 1; ? – user387184 Nov 04 '11 at 11:22
  • 1
    No variables are automatically rebuilt when this happens. Your activity receives callbacks (if you implement them) for you to save and restore your state; its entirely up to you to define what that means. – mah Nov 04 '11 at 11:26
  • 1
    I'd advice to store the values into a more persistent memory on the `onPause()` Callback and restore the values in the `onResume()` – Philipp Wendt Nov 04 '11 at 11:39