Defining Methods
Methods are also called functions
in other languages. Methods are reusable code blocks that will only execute when called. They are used to define behavior in classes.
General Form of a Method
Modifiers
The modifiers could be the access modifiers public
, private
, protected
, no modifier specified(default
)or others to be discussed later on. The access modifiers work the same as specified previously for member variables(fields
).
Example Modifiers
Since the isConnected
method is marked public
, the method will be accessible from with the class
, package
, subclass
and the world
.
public boolean isConnected(){
return true;
}
With the protected
keyword, the method will be accesible from with the class
, package
, and subclass
but not from the world
.
protected boolean isConnected(){
return true;
}
Return Type
The data type of the value returned by the method or the special keyword void
, if the method does not return a value.
Example Return Type
The method isConnected
returns a boolean value of true.
boolean isConnected(){
return true;
}
The method does not return a value, its return type is void
.
void connect(){
//some code here
}
Method Name
Any legal identifier, but conventions do restrict the method names.
Method Name Conventions
1. Method name should be a descriptive name in lowercase.
2. A multi-word name that begins with a descriptive name in lowercase.
3. In multi-word names, the first letter of each second and following words should be capitalized.
Example Method Names
send
sendMessage
connected
isConnected
setName
getName
Method Parameters
The parameters
are a comma separated list of input variable names preceded by their data types and all enclosed in parenthses,(). If there are no parameters, you must use empty parenthses, ()
.
Example Methods With and Without Parameters
The send
method takes in one parameter, message
of type String
.
void send(String message){
//some code here
}
This is a variation of the send
method that take in a message
and the senderId
parameter. The parameters are separated by a comma.
void send(String message, String senderId){
//some code here
}
Method Overloading
Defining methods with the same name but having different parameters is called method overloading. In the example above, we would have overloaded the send
method.
Calling Methods
To call a method or invoke it, the dot operator, (.
) is used. A instance of the class needs to be created first before calling a method.
When you invoke
a method, the arguments used must match the declaration's parameters in type and order.
class Socket{
boolean isConnected(){
return true;
}
void send(String message){
}
}
We can call the isConnected
method as follows from another class as follows:
Socket socket = new Socket();
if(socket.isConnected()){
socket.send("Howdy");
}
We first create a new object from the class Socket
and then use the dot operator, (.
) to call the isConnected
method. If we are connected we then call the send
method. Notice we pass in the string argment to the send
method.
The call to the methods are socket.isConnected()
and socket.send("Howdy");
Howdy
is called the method argument
.
The dot operator is only used when the method is called from outside the class. Within the method can be called without the dot operator.
class Socket{
boolean isConnected(){
return true;
}
boolean send(String message){
if(isConnected()){
//send message and return true
return true;
}
return false;
}
}
Notice we called the isConnected
method without the dot operator. This is because there's no object created as yet and we are within the class, so we can call the method without the dot operator.