I am a fresher in Android. Recently I read a lot source code about Android. I really do not understand these codes below.
public DemoActivity {
private static final String TAG = "DemoActivity";
//.........
}
Why uses static?
I am a fresher in Android. Recently I read a lot source code about Android. I really do not understand these codes below.
public DemoActivity {
private static final String TAG = "DemoActivity";
//.........
}
Why uses static?
You use static in this case because the String named TAG
is associated with the DemoActivity
class itself, not with any particular instance of DemoActivity
. In this case it is a constant referring to the name of the class, this is the same for any instance of class DemoActivity
, and so is static
.
static final String TAG = "DemoActivity"
==> This will make the string literal "DemoActivity"
a compile-time constant i.e, everywhere you use TAG
in your code, it will be replaced by the value "DemoActivity"
during compilation. i.e, in Bytecode.
Usually when we create tag we declare it like
public final static String TAG = "APPLICATION_TAG";
other use is so you can use this tag in other Activities
String getTag = DemoActivity.Tag;