Why doesn't the static variable value of the static class change?

Asked 2 years ago, Updated 2 years ago, 53 views

When I created a static class with an internal class for the purpose of using a variable without creating an object and called a static variable, I first read the value of the edit text and sprayed it in the text view, but from the second time, it still brings up the first value...

If you change it to a member class and create an object and call it up, it keeps bringing up the changed variable, but why is static not?

Below is the code you practiced.

public class MainActivity extends AppCompatActivity {

static EditText editText;

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

    editText = findViewById(R.id.edittext);
}

 public static class get{

    static String m = editText.getText().toString();


}

public void btn(View view) {

   TextView textView = findViewById(R.id.view1);

   textView.setText(get.m);

}

}

android-studio java

2022-09-20 11:12

1 Answers

static String m is a static variable. The static variable is initialized only once when the app is started. So static is usually used to define constants.

get When you say you tried the class as static:

public class get {
    String m = editText.getText().toString();
}

...

new get().m;

I think you did it like this, so whenever you do new get(), you will reset Stringm and return a new value every time, right?

If you don't know what "initialization" means:

static String m = editText.getText().toString();

You can roughly think of this line as being executed. 😆


2022-09-20 11:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.