我想知道是否有可能调用外部类的方法而不必实际声明该类的对象.他们设置的方式导致每次调用对象使用的方法时,存储在对象中的ArrayList会清空.
如果我可以在没有对象的情况下调用该方法,那么我可以解决我的问题.
提前致谢.
调用类:
public class BookingScreen extends Activity {
GAClass sendApplication = new GAClass();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_booking_screen);
}
public void saveBookingInfo(View view) {
EditText applicantNameText = (EditText) findViewById(R.id.applicantNameTextField);
EditText itemToBurnText = (EditText) findViewById(R.id.itemToBurnTextField);
String appName = applicantNameText.getText().toString();
String appItemToBurn = itemToBurnText.getText().toString();
if (appItemToBurn.isEmpty() || appName.isEmpty()) {
Toast.makeText(BookingScreen.this, "Please fill in all fields.", Toast.LENGTH_SHORT).show();
}
else {
sendApplication.storeApplication(appName, appItemToBurn);
this.finish();
}
}
外部方法类:
public class GAClass {
ArrayList peopleAttending;
public void storeApplication(String name, String item){
peopleAttending = new ArrayList<>(10);
peopleAttending.add(new Application(name, item));
}
}
解决方法:
使用静态方法.您可以在不创建封闭类的对象的情况下调用静态方法.
标签:java,object,instantiation
来源: https://codeday.me/bug/20190612/1222914.html