In this section, we will explore the concept of method in the reference of object oriented programming techniques.
Method in Java
In this section, we will explore the concept of method in the reference of object oriented programming techniques.
As we earlier described in the topic class, a class have two major parts state and behavior. States are represented by variables and behaviors are defined by methods. Methods perform the most important work in a class, object can't do anything without method. Some programming languages use reflexive data structures to do the same action, they have a complete set of data, but they don't have any behavior and due to this they depend on other functions.
Methods perform a particular task (i.e. provide a particular
functionality) achieved through coding.
Methods have two main parts:
i) declaration part it is also called the signature of the method.
ii) a body part of the method enclosed within the curly braces.
Method
Signature
Declaration part of the method consists of a name, a list of arguments, an access modifier and a return type. Return type of a method can be a primitive type or an object or a void. If the return type is void it means the method does not returns a value. In the second part of the method, body contains code that has definition for a particular task.
Let's take a look at the general syntax for a method declaration:
[modifiers]
return_type method_name (parameter_list) [throws_clause] { [statement_list] } |
Everything within square brackets [] is optional.
Let's explore the things in detail:
In java language there are some important component required for the method declaration which are as follows :
Modifier :There are four types of modifiers: public, private, protected a package which is described in the earlier topic Field.
Return type: A return type defines the type of value returned from a method. It can be a valid Java type (primitive or a class) or void (if the method does not return a value). If the method declares a return type, the exit path out of the method must have a return statement.
Method name:
There are some rules for choosing a method name like it should not use the
key words, the name of the method is
case sensitive. It
must start with a letter, dollar sign ($), or underscore (_). Subsequent
characters may be digits, letters, dollar signs or underscores. Any method should start
with a lowercase or if a method have two words then the second word's first
letter should be in capitalized form e.g. demo, test, myDemo. myTest etc. A
method has a unique name in a class but there may be more than one method of the same
name exhibiting method overloading.
Parameter must be in parenthesis:
A comma-delimited list of input parameters, preceded by their data types,
enclosed by parentheses (). If there are no parameters, we must use empty
parentheses.
Body:
The body of the method is enclosed between the curly braces. The method's code,
including the declaration of local variables, should be in the method's body.