Android - importing Java class into action

I have a java class called Constants filled with variables. And currently, if I want to use this variable in an activity, I have to use it like this.

Constants.variable = "blah blah";

      

Is there a way to import my constants class into my activity so that I can just use it like a regular variable like this

variable = "blah blah";

      

I tried

import.com.myname.appname.Constants;

      

but it doesn't work.

Thanks for the help at adavance :)

+3


source to share


4 answers


given Constants.variable - static variable

import static com.myname.appname.Constant.*;

      

this will import all your static variables in the current namespace only static variable



import static com.myname.appname.Constant.variable;

      

now you can use the variable like a regular variable

+6


source


try:

import com.myname.appname.Constants;

      



Are you using an IDE?

Also, they are, as you stated, how you intend to use the Constants class. But you shouldn't assign anything ... because the variable must be constant, i.e. static final

+2


source


Is there a way to import my constants class into my activity so that I can just use it like a regular variable like this

I don't think that's not what it is import

for.

See this SO question: Importance of Import Statement in Java File

+1


source


You must declare "variable" as static and import the class as static following the syntax

    import static com.myname.appname.Constants.*;

      

+1


source







All Articles