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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:15:15+00:00 2026-06-12T09:15:15+00:00

Simple question. Reviewing my code, I’ve noticed that I’ve got a lot of variables

  • 0

Simple question. Reviewing my code, I’ve noticed that I’ve got a lot of variables declared multiple times in my classes or methods…for example:

public Long dbInsertCheckin(final String Class) {
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
...
}

And

public class SmashDataSource {
    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    final SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
    final SimpleDateFormat timeFormat = new SimpleDateFormat("HHmm");
...
}

this got me thinking that instead of declaring “dateformat”, “sdf”, or “timeformat” or others i use in multiple places, would it not make more sense for me to declare these globally in my application class, then refer to them wherever I wanted like

public class MyApp extends Application {
public final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public final SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
    public final SimpleDateFormat timeFormat = new SimpleDateFormat("HHmm");

and refer to them later in other classes as such:

MyApp.dateformat
Myapp.sdf

Would this be better from a performance/memory usage point of view? Is there any reason not to do this? To me, it would seem that having declared them multiple times would consume more memory, vs. a final declaration once…. but I don’t know how the compiler does it’s optimizations.

  • 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-12T09:15:16+00:00Added an answer on June 12, 2026 at 9:15 am

    There is nothing wrong with using global variables. Generally they are avoided to keep things flexible and encapsulated. But some design patterns like the Factory Pattern go well with static/global classes. It also avoids code duplication.

    (However I might avoid using my Application class so my global fields are only used when needed, but that’s an implementation detail.)

    I would do something like this probably. It keeps things flexible while also putting things in one place so they are consistent across your app.

    public class MyGlobals
    {
        private static SimpleDateFormat dateFormat;
    
    
        public static SimpleDateFormat getDateFormat()
        {
            if(dateFormat== null)
            {
                dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            }
            return dateFormat;
    
        }
    }
    

    Then you can use this in other classes:

    MyGlobals.getDateFormat();
    

    ====


    Here’s some info from the dev docs about it:

    http://developer.android.com/guide/faq/framework.html#3

    For sharing complex non-persistent user-defined objects for short
    duration, the following approaches are recommended:

    Singleton class

    You can take advantage of the fact that your application components
    run in the same process through the use of a singleton. This is a
    class that is designed to have only one instance. It has a static
    method with a name such as getInstance() that returns the instance;
    the first time this method is called, it creates the global instance.
    Because all callers get the same instance, they can use this as a
    point of interaction. For example activity A may retrieve the instance
    and call setValue(3); later activity B may retrieve the instance and
    call getValue() to retrieve the last set value.

    A public static field/method

    An alternate way to make data accessible across Activities/Services is
    to use public static fields and/or methods. You can access these
    static fields from any other class in your application. To share an
    object, the activity which creates your object sets a static field to
    point to this object and any other activity that wants to use this
    object just accesses this static field.

    A HashMap of WeakReferences to Objects

    You can also use a HashMap of WeakReferences to Objects with Long
    keys. When an activity wants to pass an object to another activity, it
    simply puts the object in the map and sends the key (which is a unique
    Long based on a counter or time stamp) to the recipient activity via
    intent extras. The recipient activity retrieves the object using this
    key.

    Persistent Objects

    Even while an application appears to continue running, the system may
    choose to kill its process and restart it later. If you have data that
    you need to persist from one activity invocation to the next, you need
    to represent that data as state that gets saved by an activity when it
    is informed that it might go away.

    For sharing complex persistent user-defined objects, the following
    approaches are recommended:

    Application Preferences
    Files
    contentProviders
    SQLite DB
    

    If the shared data needs to be retained across points where the
    application process can be killed, then place that data in persistent
    storage like Application Preferences, SQLite DB, Files or
    ContentProviders. Please refer to the Data Storage for further details
    on how to use these components.

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

Sidebar

Related Questions

Simple question: Is there a class or interface that encapulates the getting of a
Simple question - How do I convert a string that has been through parseInt
Simple question: does the Java memory/synchronization model guarantee atomic pointer writes? That is, if
simple question about intent action naming convention: I have a new service that handles
Simple question that we've spent to much time on. We have a server side
Simple question. Is it possible to check the type of a variable that is
While reviewing some PHP code I've discovered a strange thing. Here is the simple
Simple question about regexps. I've got String text = foobar1foobar1; And I need to
Simple question but it is killing. How do I write an expression, that return
Simple question that probably has a very simple answer. I am writing date strings

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.