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.
Java doesn't support pass-by-reference. Primitive data types and Immutable class objects strictly follow pass-by-value; hence can be safely passed to functions without any risk of modification. For non-primitive data types, Java sends a copy of the reference to the objects created in the heap memory.
Pass-by-references is more efficient than pass-by-value, because it does not copy the arguments. The formal parameter is an alias for the argument. When the called function read or write the formal parameter, it is actually read or write the argument itself.
Since the variables are just the reference to the objects, we get confused that we are passing the reference so java is passed by reference. However, we are passing a copy of the reference and hence it's pass by value.
This intuitively looks like the pass-by-reference example above: making a change to the passed-in object affects that object's values in memory. We can think of Java intuitively as pass-by-reference for all objects. This is why we professors called Java pass-by-reference. Java is officially always pass-by-value.
It sounds like you want to have a method create four objects for you and return them. You have a few options:
Alert foo = createOneAlert(x,y,z);
Alert bar = createOneAlert(a,b,c);
// etc.
public class Alerts {
private final Alert foo;
private final Alert bar;
private final Alert baz;
private final Alert quux;
public Alerts(Alert foo, Alert bar, Alert baz, Alert quux) {
this.foo = foo;
this.bar = bar;
this.baz = baz;
this.quux = quux;
}
public Alert getFoo() { return foo; }
// etc.
}
public Alerts makeAlerts(AlertBuilder builder) {
return new Alerts(
builder.buildAlert(a,b,c),
builder.buildAlert(d,e,f),
builder.buildAlert(g,h,i),
builder.buildAlert(x,y,z));
}
It may be more verbose but a) that's Java and b) verbosity can be a good thing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With