Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the best way to organize Java code since you can't pass by reference?
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>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!</p> <p>Am I thinking about this the right way? Is there anything I can do?</p> <p>-Edit- </p> <pre><code> 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(); </code></pre> <p>and instead replace it with</p> <pre><code>createAlerts(); </code></pre> <p>and have that code off somewhere to the side.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload