Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to organize Java code since you can't pass by reference?

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.

like image 837
Adam Avatar asked Jun 13 '10 17:06

Adam


People also ask

Is there no pass by reference in Java?

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.

Which is better pass by value or pass by reference?

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.

Why Java is pass by value and not pass by reference?

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.

Is Java pass by or pass by reference?

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.


1 Answers

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.

Alert foo = createOneAlert(x,y,z);
Alert bar = createOneAlert(a,b,c);
// etc.

2. Create a data structure to hold your alerts:

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

like image 134
davetron5000 Avatar answered Oct 06 '22 23:10

davetron5000