-6

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?

stackvoid
  • 117
  • 1
  • 2
  • 9
  • 2
    You are missing the data type `private static final String TAG = "DemoActivity"` – TheLostMind Jul 19 '15 at 12:30
  • Do you know what `static` mean? If yes then what is confusing you here? – Pshemo Jul 19 '15 at 12:39
  • I'm voting to close this question as off-topic because is way too basic. Anyone with notions of this technology would not find this useful. – mdelolmo Jul 19 '15 at 13:34
  • 1
    While I agree this question should be closed, being _basic_ does not inherently make a question bad. – Adam S Jul 19 '15 at 13:46
  • possible duplicate of [What exactly does "static" mean when declaring "global" variables in Java?](http://stackoverflow.com/questions/3415781/what-exactly-does-static-mean-when-declaring-global-variables-in-java) – Adam S Jul 19 '15 at 13:47
  • @Pshemo - I think the question should be why `static final`? :) – TheLostMind Jul 19 '15 at 13:51

3 Answers3

5

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.

user4989692
  • 1,609
  • 1
  • 20
  • 25
1

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.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

Usually when we create tag we declare it like

public final static String TAG = "APPLICATION_TAG";
  1. the sole purpose of using static is to make variable exist in memory as long as Parent lifetime is there
  2. other use is so you can use this tag in other Activities

    String getTag = DemoActivity.Tag;
    
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Sanjeev
  • 4,255
  • 3
  • 28
  • 37