Java – static variables that span multiple different subclasses – corrected
•
Java
I want to know that if I define a basic activity object, all my activities are subclasses Then I declare a static variable in the base class. All subclasses will use the same static, or one for each subclass
For example My foundation:
public class MyBaseActivity extends Activity{ static int myStatic; ... .... }
then:
public class MyActivity1 extends MyBaseActivity { private void someMethod1(){ myStatic = 1; } ... .... }
and
public class MyActivity1 extends MyBaseActivity { private void someMethod2(){ if (myStatic == 1) doSomething(); } ... .... }
If I start myactivity1 now and set a value in "mystatic" Then it exits, and then I start myactivity2 - if I still have the value set by the first activity? In the above example, is the "if" statement true or false?
I know that if I instantiate activity1 multiple times, obviously I will get the same static variable However, I instantiate a different subclass every time
I get the impression that this is happening to me, but I want to be sure
Solution
Static is static They will reference the same object
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码