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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:09:09+00:00 2026-06-11T23:09:09+00:00

Possible Duplicate: Why are static variables considered evil? I have the habit of using

  • 0

Possible Duplicate:
Why are static variables considered evil?

I have the habit of using static variables extensively in all my programs, especially when I am working with Android. I tend to use them because sometimes it feels so cumbersome to send 10 or more values via Intents. So, I just declare them as static variables and access them in the other classes easily by using the “dot” operator. Another reason for using static variables is when I am making a Utility class to be used throughout my application. Like the code I have given below helps me to use the variables in different activities.

Utility.java

public class Utility {
public static Facebook fb;
public static AsyncFacebookRunner fbAsyncRunner;
public static String[] fbPermissions = {"email", "read_stream", "user_birthday"};
public static final String PREF_UTILITY_FILE_NAME = "PrefUtilityFile";
public static SharedPreferences prefs;
public static Editor editor;
public static String access_token;
public static long expires;
}

I searched online for similar questions and came across this and this, but they do not seem to give a final answer to the issue. And in most of the places, I see conflicting opinions and hence am totally confused.

Is it a good programming practice or bad ? Should I be using it or not ?

  • 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-11T23:09:11+00:00Added an answer on June 11, 2026 at 11:09 pm

    You can replace all you static fields with a “Context” object which you can pass around or make a Singleton. It is possible to remove almost all your static fields away. Whether this is a good idea or not is up to you, but I wouldn’t assume that it has to be much harder to use instance fields.

    BTW: I would suggest

    • placing static fields/constant with the class or package which uses them
    • treat static arrays as immutable if possible making them final as well.

    You can use a non-static Context with

    public class Context {
        public static final String PREF_UTILITY_FILE_NAME = "PrefUtilityFile";
    
        public Facebook fb;
        public AsyncFacebookRunner fbAsyncRunner;
        public String[] fbPermissions = {"email", "read_stream", "user_birthday"};
        public SharedPreferences prefs;
        public Editor editor;
        public String access_token;
        public long expires;
    }
    
    // pass to constructor as required
    class UsesContext {
        final Context context;
        public UsesContext(Context context) {
            this.context = context;
        }
    
        public void method() {
            // can use context
        }
    }
    

    This allows you to create unit tests with multiple Contexts.

    The only thing I would leave static are constants.

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

Sidebar

Related Questions

Possible Duplicate: Static Way to get Context on android? I have a broadcast receiver
Possible Duplicate: Static Variables in WCF So I am fairly new to using WCF
Possible Duplicate: accessing static member variables I have a symbol inst which is an
Possible Duplicate: Where do I have to declare static variables? I've seen code like
Possible Duplicate: Is the C# static constructor thread safe? I have multiple threads using
Possible Duplicate: Overriding static variables when subclassing I have a set of classes that
Possible Duplicate: C++ static virtual members? Can we have a static virtual functions? If
Possible Duplicate: Why global and static variables are initialized to their default values? What
Possible Duplicate: non static method cannot be referenced from static context hey i have
Possible Duplicate: Referring to a static member of a subclass Please have a look

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.