I’m learning how to code in Java after after coming from C. In C I always separated everything into individual functions to make the code easier to follow and edit. I was trying to do this in java but now since I realized that you can’t use pointers, I am a bit confused as to what the best way to do this is.
So for example I want to have a method that creates four alerts for me. So I pass it an alert builder that can then create the alerts. I can return them in an array, but in my code I already have the alerts individually named, and I would like to keep it that way so I wouldn’t need to refer to them as alert[1], alert[2]… etc.
So that means I would have to rename them, which would add additional code which would probably be longer than the code in the actual method!
Am I thinking about this the right way? Is there anything I can do?
-Edit-
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(this.getString(R.string.ache_Q_text))
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {dialog.cancel();}
});
final AlertDialog ache_Q_alert = builder.create();
builder.setMessage(this.getString(R.string.food_Q_text));
final AlertDialog food_Q_alert = builder.create();
builder.setMessage(this.getString(R.string.event_Q_text));
final AlertDialog event_Q_alert = builder.create();
builder.setMessage(this.getString(R.string.ache_Q_text));
final AlertDialog ache_type_Q_alert = builder.create();
and instead replace it with
createAlerts();
and have that code off somewhere to the side.
It sounds like you want to have a method create four objects for you and return them. You have a few options:
1. Modify your method to create only one object, and call it four times, e.g.
2. Create a data structure to hold your alerts:
It may be more verbose but a) that’s Java and b) verbosity can be a good thing