Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6771697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:27:46+00:00 2026-05-26T15:27:46+00:00

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

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:27:46+00:00Added an answer on May 26, 2026 at 3:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class defined as: class Obj { public: int width, height; Obj(int
How can I add an instance variable to a defined class at runtime ,
I defined next class with virtual properties: public class Order: BaseEPharmObject { public Order()
I have a class defined as public class viewGroups extends ListActivity Somewhere in the
add unique Elements to List of User defined class (i.e. List ) public class
My Grails 2.0 app has a User domain object defined: class User { static
In a class called 'Quality' I have the following constants defined: class Quality <
I am trying to expose a single well defined class by building a static
I have three different base classes: class BaseA { public: virtual int foo() =
I have some public variables that are defined as follows: public class FieldsToMonitor {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.