0

If I press the back button and start the activity again (two times), the hashcode is different, but the static variable assigned is retained. It creates two different objects. How does this work?

public class MainActivity extends AppCompatActivity {

private static int myStatic = 1;
private int my = 1;
private Button button = null;
private MyStaticClz myStaticClz = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if(myStaticClz == null) {
        myStaticClz = new MyStaticClz();
        Log.v("myStaticClz: ", myStaticClz.toString());
    }

    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            myStatic = 2;
            my = 2;
            myStaticClz.checkStatic = 2;
            myStaticClz.check = 2;
        }
    });

    Log.v("myStatic: ", String.valueOf(myStatic));
    Log.v("my: ", String.valueOf(my));
    Log.v("checkStatic: ", String.valueOf(myStaticClz.checkStatic));
    Log.v("check: ", String.valueOf(myStaticClz.check));
}

public static class MyStaticClz {
    public static int checkStatic = 1;
    public int check = 1;
}
}

Output:

04-18 18:10:42.599 3357-3357/com.xyz.state V/myStaticClz::    com.xyz.state.MainActivity$MyStaticClz@fbd3691
04-18 18:10:42.599 3357-3357/com.xyz.state V/myStatic:: 2
04-18 18:10:42.599 3357-3357/com.xyz.state V/my:: 1
04-18 18:10:42.599 3357-3357/com.xyz.state V/checkStatic:: 2
04-18 18:10:42.599 3357-3357/com.xyz.state V/check:: 1
04-18 18:10:55.738 3357-3357/com.xyz.state V/myStaticClz::     com.xyz.state.MainActivity$MyStaticClz@c1666fb
04-18 18:10:55.738 3357-3357/com.xyz.state V/myStatic:: 2
04-18 18:10:55.738 3357-3357/com.xyz.state V/my:: 1
04-18 18:10:55.738 3357-3357/com.xyz.state V/checkStatic:: 2
04-18 18:10:55.738 3357-3357/com.xyz.state V/check:: 1
powder366
  • 4,351
  • 7
  • 47
  • 79

1 Answers1

3

This is the difference between static (class) variable and instance variable. https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory

Evgeny
  • 2,483
  • 1
  • 17
  • 24
  • Actually I knew the answer thinking about it. To quick to ask, feel a bit stupid:-) Where can I dig to find more info how it works behind the scenes? – powder366 Apr 18 '16 at 16:48
  • Good read: http://stackoverflow.com/questions/708012/how-to-declare-global-variables-in-android?rq=1 – powder366 Apr 18 '16 at 16:58
  • 1
    @powder366 Behind the scene in JVM the special memory area (aka `PermGen`) is used to store classes and its associated data like static variables. Dalvik also has the similar area called `LinearAlloc`. http://stackoverflow.com/questions/22299403/whats-the-data-in-dalvik-linearalloc-dalvik-aux-structure-dalvik-bitmap-1-da – Evgeny Apr 18 '16 at 17:03