Search code examples
javaandroidnotifynotificationmanager

possible to return values from parameters in Java?


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


Solution

  • 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.