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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:20:05+00:00 2026-06-02T01:20:05+00:00

The entire original question was based on a mistake. You can NEVER assign the

  • 0

The entire original question was based on a mistake. You can NEVER assign the value of a final static FIELD constant in the constructor. If you could, you would have the value of the static FIELD changing every time a new instance of the class was created. Not very final!

I can instead do what I have been doing (and possibly creating memory leaks, I have a lot to learn), which is to use a static (but not final) field to point to the most recent instance of my Activity (see the code sample below). As the comment below that talks of memory leaks points out, I almost certainly have been creating memory leaks by keeping references to my older Activity objects around.

So the answers to my original question really comes down to: this is not a good thing to do because handing other objects references to my current Activity object allows them to keep the system from ever garbage collecting my old Activity objects!

Anyway, here is the code referred to in my original question, the answers and comments are still here so the rest of this is kept for reference to those.

public class MyActivity extends Activity {
  public final static MyActivity uI;
  public static MyActivity mostRecentUI;

  private MyActivity() { // this is the constructor
      // In my original post I had:
      // uI = this;  
      // but this is illegal code.  `final static anything;` cannot be assigned a 
      // value in the constructor, because it could be assigned a different value
      // each time the class is instantiated!  

      // Instead the best I can do is
      mostRecentUIobject = this;
      // and this name better describes what this static variable contains
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  }
}

Added 4/15: @dmon goes as far as to comment that the constructor won’t be run. It will, and anybody with any doubt can run this test activity:

public class TestActivityConstructor extends Activity {
    static long refTime = System.currentTimeMillis();
    static String staticExecution = "Static never executed\n";
    static String constructorExecution = "Constructor never executed\n";
    static String onCreateExecution = "onCreate never executed\n";

    static {
        staticExecution = "Static Execution at " + (System.currentTimeMillis() - refTime) + " ms\n";
    }

    public TestActivityConstructor() {
        constructorExecution = "Constructor Execution at " + (System.currentTimeMillis() - refTime) + "ms \n";
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        onCreateExecution = "onCreate Execution at " + (System.currentTimeMillis() - refTime) + "ms \n";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ((TextView) findViewById(R.id.TV)).setText(staticExecution + constructorExecution + onCreateExecution);
    }
}

Obviously, you need a trivial layout.xml with a textview called TV. No permissions needed. You can even have fun rotating your android to see the app recreated showing that both the constructor and the onCreate are re-run every time the screen is rotated, but that the static assignments are NOT redone when the Activity is recreated.

  • 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-02T01:20:06+00:00Added an answer on June 2, 2026 at 1:20 am

    To basically answer the main part of your question title…

    OK to use private constructor in Activity…?

    No.

    I’m assuming you’re coming to Android from a previous Java stand-point but one thing to understand about Android is that the Activity class is one of a few special case classes which should be left to work as they’re designed to.

    Don’t concern yourself with the concept of constructors when it comes to the Activity class just follow the design methodologies and application fundamentals which work quite adequately.

    If you have a specific requirement which doesn’t work within the Android framework then post a question with example code and somebody will explain how to fix it.

    EDIT: In light of your update to your question…

    public final static MyActivity uI;
    

    With this in place, I can refer back to the “user interface” object as MyActivity.uI without having to pass references through chains of calls.

    Seriously…don’t do that. There are perfectly adequate, functional and safe mechanisms which can be used to easily handle any situation with minimal code without ‘breaking’ the model in the way you’re attempting to do things.

    To repeat what I said above in my original answer – the Android Activity class is a special case. Instances of Activity and their methods/fields are not meant to be accessed directly by any other external class.

    You’ll be doing yourself a favour and save a lot of headaches if you simply accept the fundamentals of the Android framework. Coding Android apps may well involve Java but not all ‘generic’ Java concepts apply, in particular to some of the core components such as Activity.

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

Sidebar

Related Questions

Original Question: I am new to SQL server and can't quite find what I
Can i pass the entire POST array into a function and handle it within
EDIT: The entire code and database creation script can be found from http://gitorious.org/scheator .
this is my entire PHP code: <?php if(empty($_POST['selid'])) {echo no value selected; } else
I am reposting this question due to inability to solve the problem (original here
Ok, i kind of asked the wrong question so I've edited the original question.
I edited my original text to demostrate my entire set of code for those
Just a newbie question really. Can you edit a website (in Visual Studio) on
This is a homework question, I got the basics down, but I can't seem
The original question I'm new to STM. One thing I'd like to do in

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.