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 7718815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:15:34+00:00 2026-06-01T03:15:34+00:00

public class Checker { static private int value1 = 0, value2 = 1; static

  • 0
public class Checker {
    static private int value1 = 0, value2 = 1;

    static private Activity activity;

    public static void init(Activity activity) {
        Checker.activity = activity;

        value1 = // calculate value

        value2 = // calculate value, normally the same as value1
    }

    public static void check() {
        if (value1 != value2)
            activity.finish();
    }
}

I got the above class and placed a call to Checker.init() in onCreate of the main activity and calls to Checker.check() at various points of my code. Problem is that I get a NullPointerException in Checker.check() for activity.finish() when re-opening the app after it has been backgrounded for some time.

Normally value1 == value2, so this exception seems to indicate that all the static variables were reset to default. I am a Java/Android newbie, but I thought these variables are retained as long as the activity is in memory and in case the activity is killed onCreate and so Checker.init() called again. So why does this happen?

  • 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-06-01T03:15:35+00:00Added an answer on June 1, 2026 at 3:15 am

    Based on the symptoms you are describing, what’s most likely happening is that the ClassLoader for Checker is getting garbage collected but your Activity is not being garbage collected. So when the app returns from the background, Activity goes to onResume. When the call to Checker.check() is made, the Checker class is reloaded with the default values of value1 and value2.

    One way to make sure that the Checker class stays around as long as the Activity is to keep a reference to a Checker instance in the Activity class. Of course, you might also consider redesigning the Checker functionality so that it doesn’t depend on static member behavior.

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

Sidebar

Related Questions

public class Ex7 { private int fld; private void meth(int val) { fld =
MainLexer.h in ‘Checker’ Project of static Library class MainLexerFuncs { private: static int run();
public class TestBL { public static void AddFolder(string folderName) { using (var ts =
public class UploadHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { ... // Create
public class ToolPartGetLists : Microsoft.SharePoint.WebPartPages.WebPart, ICommunicationInterface { private bool _error = false; //......... protected
public class Order { static Customer cust = new Customer(); string sEmpty = ;
public class MySingletonClass { public MySingletonClass() { _mySingletonObj = Instance(); } public static MySingletonClass
public class Test extends Thread{ public void hello(String s){ System.out.println(s); } public void run(){
public class Options { public FolderOption FolderOption { set; get; } public Options() {
public class A { //some fields (may not only primitive types) and methods here}

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.