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
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. 😆
574 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
579 Understanding How to Configure Google API Key
925 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
577 Who developed the "avformat-59.dll" that comes with FFmpeg?
617 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.