4

I have a java class called Constants filled with variables. And currently if I want to use that 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 normal variable like this

variable = "blah blah";

I have tried

import com.myname.appname.Constants;

but this doesn't seem to work.

Thanks for the help in advance.

itsmysterybox
  • 2,748
  • 3
  • 21
  • 26

4 Answers4

6

considering Constants.variable is a static variable

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

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

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

now you can use variable like a normal variable

2

try:

import com.myname.appname.Constants;

Are you using an IDE?

Also, they way you have stated is how you are suppose to use a Constants class. But you are not suppose to assign anything... because the variable are suppose to be constant, ie. static final

tachyonflux
  • 20,103
  • 7
  • 48
  • 67
2

You have to declare "variable" as static and import the class as static by following syntax

    import static com.myname.appname.Constants.*;
jafarbtech
  • 6,842
  • 1
  • 36
  • 55
1

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

I dont think so, its not what import for.

See this SO question : Meaning of the import statement in a Java file

Community
  • 1
  • 1
Blaze Tama
  • 10,828
  • 13
  • 69
  • 129