1

I need to access android global(Application) variable from java class which is inside the my application.I tried with using Context ,but unable.Is there a way to do this ? Below is the my Application class.I am adding some values for jsonUrl inside my activity.So i need to access these values from a java class.

public class Application extends Application {


    private static String jsonUrl;


    public static String getJsonUrl() {
        return jsonUrl;
    }

    public static void setJsonUrl(String jsonUrl) {
        SPHApplication.jsonUrl = jsonUrl;
    }

}

Then i tried to get this valus using below code in my java class.

Application.getJsonUrl();

But it doesn't give me the valus?

1 Answers1

0

It depends.

You could use a static variable by using the static keyword. Then, by using MyClass.staticVariable, you can access it.

Another way is to subclass the android.app.Application class and store your variables there. Then, retrieve them whenever you need.

How to declare global variables in Android?

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221