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