All About Methods in Java




Method:
Parameter names are arbitrary but meaningful identifiers for types that are passed to the methods. Methods may or may not have parameters.

Arguments represents actual values passed to method when it is executed so that it can use them for some specific purpose within the implementation of the method.The data types defined in the implementation language of the class or it can be class type.

The return type is the type that the method returns when method is finished its execution. The return is also primitive data types(defined in Java). On the other hand there may not be any return type . In that case it will be void. Void says that the method does not return any value when it finishes its execution.
Method declaration syntax:

methodName(parameterList) throws ExceptionLists
//like
main(String args[]) throws ArrayOutOfBoundException,IllegalThreadStateException
 
Method implementation

access_specifier modifier returnType methodName(parameterList) throws ExceptionLists{
//body
}

//like
public static void main(String args[]) throws ArrayOutOfBoundException,IllegalThreadStateException{
//body
}
 
Method calling general Syntax:

object_reference.methodName();
object_reference.methodName(paramName1,paramName2......,paramNameN);
//Example:
Student std=new Student();
std.setName("abc");
std.getName();

 
a method can not be passed as an argument to another method or in constructor.Instead an object reference is passed to the method or constructor so that the object reference is made available to that method or constructor or to other members of the class that invokes the method.The single parameter is replaced by a computable value i.e an argument when the method is invoked and executed.
Return types of a method:
Java methods can return one of the following:

  • Void- if the method does not return anything
  • One of the java's primitive datatypes
  • A class object
The return keyword has two objectives:

  1. It indicates that the method is done,no statement after return will be executed.
  2. It produces a value and passed right after the return statement.
If we wish to return nothing from a method then we can do the followings:
  1. We have to remove return statement.
  2. We have to write void instead of any datatype. 

The return statement in second and third cases must match the return type specified in the deceleration of the method.In that it must return a value of the same type as the return type.If stated void there should not be any return statement in the method body.
The argument list
The method argument list specifies what info we will pass into the method. The argument list is nothing but form of object.So what we pass via argument list is/are the handle(s)[Exception-the primitive datatypes(boolean,char,byte,short,int,long,float,double)].

int getSize(String str)
{
return str.length();
}
 
This argument is of type String and called str,passed into the method to find the size of str to the method called getSize().length() method is defined in the String class that returns the number of characters present in the string.

General Subroutine/method contract
The precondition of a subroutine is something that must be true when the subroutine is called to work it correctly.This is the primary part of the contract. If we do not meet the contract correctly, there is no point to execute the subroutine as the result may be wrong or the program may crash.
The post condition of a subroutine is another side of the contract.It is something that will be true after the subroutine has run(if the precondition is met).Subroutines are often described by comments that explicitly specify their precondition and post condition. So a subroutine comes with a set of precondition and post conditions.
They can be part of a javadoc commands as well as @precondition or @postcondition tags.
All About Methods in Java All About Methods in Java Reviewed by Animesh Chatterjee on November 16, 2018 Rating: 5

No comments:

Powered by Blogger.