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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:06:47+00:00 2026-06-03T20:06:47+00:00

From several classes in my app I want to get String global values without

  • 0

From several classes in my app I want to get String global values without using Context.
If I declare this variables in Application class, SharedPreferences or Strings.xml, then I must use Context for get/set it values.

Using Singleton is not a good practice as I understand from this post.
Is there any good way for using global variables without using context?

  • 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-03T20:06:48+00:00Added an answer on June 3, 2026 at 8:06 pm

    Create a global class or store the string in a place that “makes sense”. For instance if you have the class Shoes you could do this:

    public class Shoes {
    
        public static class BRAND {
            public static final String NIKE = "nike";
            public static final String REBOK = "rebok";
            public static final String ADDIDAS = "addidas";
        }
    
        private String brand;
    
        public Shoes() {}
    
        public void setBrand(String brand) {
            this.brand = brand;
        }
    
        public String getBrand() {
            return this.brand;
        }
    }
    

    Now you can do this:

    public static void main(String[] args) {
    
        Shoes myShoes = new Shoes();
        myShoes.setBrand(Shoes.BRAND.NIKE);
    }
    

    You will find there are many things like this in Android. It would be even better if you used enums instead. Hope this helps.

    UPDATE

    If you would like to use setters and getters then there are 2 solutions:

    1. The first you would need an instance of the object and since you only want a single instance a singleton design pattern would be required. Really if you synchronize correctly and the design makes sense they can be very good and useful. In your situation I don’t think it would be worth the work.
    2. You can take advantage of the static initializer and static methods. You could just remove the final declaration and do what you want with the Strings, like this:

      public class Shoes {
      
          public static class BRAND {
              public static String NIKE;
              public static String REBOK;
              public static String ADDIDAS;
      
              static {
                  NIKE = "nike";
                  REBOK = "rebok";
                  ADDIDAS = "addidas";
              }
          }
      
          private String brand;
      
          public Shoes() {}
      
          public void setBrand(String brand) {
              this.brand = brand;
          }
      
          public String getBrand() {
              return this.brand;
          }
      }
      

    Or use good encapsulation practices and do this:

    public class Shoes {
    
        public static class BRAND {
            private static String NIKE;
            private static String REBOK;
            private static String ADDIDAS;
    
            static {
                NIKE = "nike";
                REBOK = "rebok";
                ADDIDAS = "addidas";
            }
    
            public static String getNIKE() {
                return NIKE;
            }
    
            public static void setNIKE(String name) {
                NIKE = name;
            }
    
            public static String getREBOK() {
                return REBOK;
            }
    
            public static void setREBOK(String name) {
                REBOK = name;
            }
    
            public static String getADDIDAS() {
                return ADDIDAS;
            }
    
            public static void setADDIDAS(String name) {
                ADDIDAS = name;
            }
        }
    
        private String brand;
    
        public Shoes() {}
    
        public void setBrand(String brand) {
            this.brand = brand;
        }
    
        public String getBrand() {
            return this.brand;
        }
    }
    

    However I must note: If you are doing this because you cannot get a Context then you are missing something. A context can be obtained from anywhere – objects instantiated by the system are given a context as a parameter. If you have your own custom object you can just pass the ApplicationContext as a parameter or the class using the object itself (this).

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

Sidebar

Related Questions

This is for a web project so i have several classes that inherit from
I have several classes, that all derives from SuperClass. When the classes are created,
I need to get data from several tables, so I used a query with
In my application I load information from several (6-10) websites using NSXMLParser and then
In my app, I have deal with several different parameters, which derive from IParameter
Part of this app is a Scream button that plays random screams from cast
I'm building an iPhone app that aggregates data from several different data sources and
The app I'm developing needs to be able to pull from several different data
I want to generate several SQL statements based on a column list using the
Writing a PHP app and have several classes that only have static methods (no

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.