user3692917
user3692917

Reputation:

Android - Import Java Class into Activity

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.

Upvotes: 4

Views: 22842

Answers (4)

jafarbtech
jafarbtech

Reputation: 7015

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

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

Upvotes: 2

Munawwar Hussain Shelia
Munawwar Hussain Shelia

Reputation: 1364

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

Upvotes: 6

Blaze Tama
Blaze Tama

Reputation: 10948

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

Upvotes: 1

tachyonflux
tachyonflux

Reputation: 20211

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

Upvotes: 2

Related Questions