2

Static variables or static method can be called without creating its class instance. Which means its values are same for all objects or any reference to that class. Does it mean we can get static values from class anywhere in our project. In my case when user login I get all of the profile information and assign it to static variables in UserProfile.java class. Can these values be accessed anywhere in my project. I don't want to use Shared-preferences or SQlite for storing this data.

3 Answers 3

2

Yes, static variables can be accessed directly from the class' name (MyClass.myVariable), as long as their access policy permits it.

Sign up to request clarification or add additional context in comments.

Comments

1

Static variables are accessible directly from class without creating an object of class.

  • Variables you want to access must be public

you can simply access them like

YourClass.yourVariable

Comments

1

If those static values are public then you can access them from wherever you can access your class. However, you cannot access classes across different modules, so to make that happen , you have to add compile project(':project_name') in your dependencies inside your gradle file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.