0

as I know, Java always invoke a method by "pass-by-value". but I see the reference for Android's NotificationManager.notify(String, int, Notification):

Returns

 the id of the notification that is associated with the string

identifier that can be used to cancel the notification

please refer to the reference: http://developer.android.com/reference/android/app/NotificationManager.html

How could this happend? Is there something I mis-understand?

BR, Henry

Henry
  • 942
  • 3
  • 11
  • 21
  • 1
    Looks to me that the documentation is wrong. It is a `void` method so it returns nothing. – michael.kebe Nov 12 '10 at 08:25
  • Could it be that simply the docs are wrong? E.g. see the other notify method... or maybe the method is different as described in the docs: int notify(string, Notification) ? Another possibility would be that the id is saved to the specified Notification parameter. – Karussell Nov 12 '10 at 08:27

4 Answers4

3

About this statement:

"Java passes primitives by value, but passes objects by reference."

This is not exact. Java passes everything by value and it doesn't pass objects at all.

  • for primitives: copies are transmitted to methods (don't forget that String is not a primitive) - it's correct what you said
  • for reference variables: they are also transmitted by value: a copy of the reference variable is transmitted to the method. So the object itself is never transmitted. The object can be altered in the method (by invoking some of its methods) and you will see the modifications after returning from the method (e.g. you change the "name" member from a Person object), but if you change the reference, this change will not be visible outside the method:

changing the reference is made by "new" operator or by an assignment like param = some_other_reference (where some_other_referece points to some other object on the heap). Changing the reference will not impact the "original" reference, but only the "copy reference" (the reference used inside the method).

Paul Ianas
  • 501
  • 3
  • 4
  • 2
    I think you meant to send this as a comment to my answer. Here it's misleading as it appears like you try to answer the OP's question. This is correct, but see my comment for clarification. – haylem Nov 12 '10 at 10:07
  • Sorry. You are right, haylem. I should have posted this under your comment. – Paul Ianas Nov 16 '10 at 07:30
0

Seems like the API Reference for NotificationManager is a bit messed up.

Here's the code as found via Google Code Search on NotificationManager and Android:

/**
 * Persistent notification on the status bar,
 *
 * @param tag An string identifier for this notification unique within your
 *        application.
 * @param notification A {@link Notification} object describing how to
 *        notify the user, other than the view you're providing. Must not be null.
 * @return the id of the notification that is associated with the string identifier that
 * can be used to cancel the notification
 */
public void notify(String tag, int id, Notification notification)
{
    int[] idOut = new int[1];
    INotificationManager service = getService();
    String pkg = mContext.getPackageName();
    if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
    try {
        service.enqueueNotificationWithTag(pkg, tag, id, notification, idOut);
        if (id != idOut[0]) {
            Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
        }
    } catch (RemoteException e) {
    }
}

Obviously the parameter doesn't return a value. They meant to have a similar JavaDoc but probably made a mistake.

Look at the code for the other variant of notify:

/**
 * Persistent notification on the status bar,
 *
 * @param id An identifier for this notification unique within your
 *        application.
 * @param notification A {@link Notification} object describing how to
 *        notify the user, other than the view you're providing. Must not be null.
 */
public void notify(int id, Notification notification)
{
    notify(null, id, notification);
}

As you can see, this overloaded version just calls the primary implementation with a default tag String value of null.


Regarding the general question of passing by value and passing by reference, the simple/vulgarized explanation is:

  • Java passes primitives by value,
  • but passes objects by reference.

Refer to the comments by arnivan and Patrick for clarification.

haylem
  • 22,460
  • 3
  • 67
  • 96
  • 1
    actually - in the true sense even object references are passed by value. when you pass a reference to a method, a copy of the reference is passed, not the same reference. in java, there is **only** pass-by-value. – anirvan Nov 12 '10 at 08:46
  • @anirvan: That's right, it has been covered in this forum before as well, [Is Java pass by reference](http://stackoverflow.com/questions/40480/is-java-pass-by-reference/116249) – Patrick Nov 12 '10 at 09:00
  • @arnivan: It is indeed correct, forgive my simplication, but in general when people ask this question, it's the expected answer, even though it's not technically correct. If you get a job interview question asking this and you only mention pass by value, you'll get shot down and may not have the time to clarify it. Easier to state it that way, and then explain that actually object's references are values themselves. – haylem Nov 12 '10 at 10:05
0

The documentation seems wrong to me. The declaration says:

public void notify (String tag, int id, Notification notification)

but at the same time it says that it returns something.

I interpret it as this: the id uniquely maps to the notification in question, and can be used when canceling the notification.

aioobe
  • 413,195
  • 112
  • 811
  • 826
0

Agree with previous posters: the doc seem to be incorrect. The method needs three parameters but in the javadoc only two of them are mentioned. Just replace @return through @param and you've got the description for the id parameter:

the id of the notification that is associated with the string identifier that can be used to cancel the notification

edit: you can define this id yourself and use it later to cancel the notification.

vitamoe1.6
  • 148
  • 1
  • 1
  • 4