next up previous
Next: Variables Up: Unit 02 Previous: Evaluation of the expressions

Static methods

Static methods are methods that do not require an invocation object. The syntax of the call of a static method is the following:

ClassName.methodName(parameters)

where:

Calling a static method is similar to calling a non-static method, except that we do not have to specify the invocation object but just the parameters. Note that the name of the method is preceded by the name of the class to which the method belongs. This is because in Java methods have a name that is local to the class and hence, to be able to identify the method, it is necessary to specify the class of the method.

Example:

JOptionPane.showInputDialog("Insert a string")

is the call of the method showInputDialog() defined in the predefined class JOptionPane. This method is passed the parameter "Insert a string" of type String. This method opens a dialog window which requires user input. Such a window displays the message "Insert a string" and an input fields where we can type in a string that the method will return. We will see this method in more detail later on.

Note: the main() method of a class is a static method that has void as return type (i.e., it does not return anything) and has as parameter an array of String objects (see later).


next up previous
Next: Variables Up: Unit 02 Previous: Evaluation of the expressions