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
2
  • 3
    Possible duplicate of Java Static vs Instance Commented Apr 18, 2016 at 16:30
  • Yes. Sorry, but it might help Android developers still. Commented Apr 18, 2016 at 16:34

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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 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. stackoverflow.com/questions/22299403/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.