What does the prefix “on” in a function (e.g.”onButtonPressed”) mean

namingnaming-standards

Sometimes I'll see some functions use "on" as a prefix, for example, on a button listener:

onButtonPressed();

and there are other examples:

onStateChanged();
onNotified();
onResume();

Does the prefix "on" have an implied semantic meaning behind it?

Best Answer

Prefix "on" is most often used to indicate that method is intended to be used as a callback, i.e. not called directly, but set as a handler for some event.

For example, when you write method named onClick, you probably won't call it directly, but rather expect that GUI toolkit will call it once user clicks a button.

Related Topic