top

Search

Java Tutorial

A method is a set of statements that perform aparticular task and return the result/outcome to the caller. A method can also perform some specific task without returning anything to the caller. Methods enable us to reuse the code without the code being retyped. In Java language, every method must be part of some class.Methods are time savers and assist us to reuse the code without the code being retyped.Method Name: Method names are also covered by field names regulations, but the convention is somewhat distinct. Modifier-: It basically defines access scope of the method i.e. from where we can access methods in your application. There are four types of access specifiers in java. private: It can access only within the class in which it is defined. default: No such access modifier. If no access identifier is provided, defaultwill be used. It can access within the same class and package that defines its class. public: It has the highest scope and can be obtained in any class in your application. protected: We can access/use protected methods within the class in which it is declared and in its subclasses. The return type:  When we call any method, it returns some data or simply executes some piece of code & returns nothing. So, thetype of the data we returnis method return type. Parameter list: Within the attached parenthesis, commas separated list of input parameters are described, preceded by their data type. You must use blank parentheses () if there are no parameters. Exception list : Method can throw the exceptions and you can indicate these exceptions in method declaration. Method body : Method body is enclosed between braces and it contains the code you need to execute to perform your intended tasks. Method signature: The signature includes the method name and parameter list (no. of parameters, type of the parameters and order of the parameters). Return type and exceptions are not considered to be part of the signature. Method naming convention: A method name is typically a single word that should be a low-case or multi-word verb, starting with a lower-case verb followed by an adjective, noun, etc. The first letter of every word should be capitalized after the first word. For example, findSum,getMax, setY and getY. Generally speaking,a method has a unique name within the class in which it is defined, but sometimes a method may have the same name as another method name within the same class as Java allows method overloading. Defined method needs to be called by other methods to use its functionality.A method returns to the code that invoked it when: It completes all the statements that are defined in that method. It returns something. It throws an exception. Parameters in java methods:  Pass by Value: The value of the method parameter is copied to another variable and then transferred/passed to the method, which is why it is called pass by value. Pass by Reference:  An alias or reference to the actual parameter is passed to the method, that’s why it’s called pass by reference. All primitive data types such as long, char, short, etc are passed by value; however, all non-primitives such as List/String/Objects are always passed by references or copy of references. Recursion  The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. Using recursive algorithms, certain problems can be solved quite easily.  returntypemethodname(){ //code to be executed   methodname();//calling same method    } Factorial Example: public class Recursion {        static int factorial(int n){                  if (n == 1)                    return 1;                  else        return(n * factorial(n-1));            }        public static void main(String[] args) {    System.out.println("Factorial of 8 is: "+factorial(8));    }    }  
logo

Java Tutorial

Java Methods

A method is a set of statements that perform aparticular task and return the result/outcome to the caller. A method can also perform some specific task without returning anything to the caller. Methods enable us to reuse the code without the code being retyped. In Java language, every method must be part of some class.

Methods are time savers and assist us to reuse the code without the code being retyped.

  • Method Name: Method names are also covered by field names regulations, but the convention is somewhat distinct. 
  • Modifier-: It basically defines access scope of the method i.e. from where we can access methods in your application. There are four types of access specifiers in java. 
  • private: It can access only within the class in which it is defined. 
  • default: No such access modifier. If no access identifier is provided, defaultwill be used. It can access within the same class and package that defines its class. 
  • public: It has the highest scope and can be obtained in any class in your application. 
  • protected: We can access/use protected methods within the class in which it is declared and in its subclasses. 
  • The return type:  When we call any method, it returns some data or simply executes some piece of code & returns nothing. So, thetype of the data we returnis method return type. 
  • Parameter list: Within the attached parenthesis, commas separated list of input parameters are described, preceded by their data type. You must use blank parentheses () if there are no parameters. 
  • Exception list : Method can throw the exceptions and you can indicate these exceptions in method declaration. 
  • Method body : Method body is enclosed between braces and it contains the code you need to execute to perform your intended tasks. 

JAVA METHODS

Method signature: The signature includes the method name and parameter list (no. of parameters, type of the parameters and order of the parameters). Return type and exceptions are not considered to be part of the signature. 

Method naming conventionA method name is typically a single word that should be a low-case or multi-word verb, starting with a lower-case verb followed by an adjective, noun, etc. The first letter of every word should be capitalized after the first word. For example, findSum,getMax, setY and getY. 

Generally speaking,a method has a unique name within the class in which it is defined, but sometimes a method may have the same name as another method name within the same class as Java allows method overloading. 

Defined method needs to be called by other methods to use its functionality.
A method returns to the code that invoked it when: 

  • It completes all the statements that are defined in that method. 
  • It returns something. 
  • It throws an exception. 

Parameters in java methods:  

  • Pass by Value: The value of the method parameter is copied to another variable and then transferred/passed to the method, which is why it is called pass by value. 
  • Pass by Reference:  An alias or reference to the actual parameter is passed to the method, that’s why it’s called pass by reference. 

All primitive data types such as long, char, short, etc are passed by value; however, all non-primitives such as List/String/Objects are always passed by references or copy of references. 

Recursion 

 The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. Using recursive algorithms, certain problems can be solved quite easily.  

returntypemethodname(){   
//code to be executed   
methodname();//calling same method   
 }  

Factorial Example: 

public class Recursion {   
    static int factorial(int n){       
          if (n == 1)       
            return 1;       
          else       
return(n * factorial(n-1));       
    }       
public static void main(String[] args) {   
System.out.println("Factorial of 8 is: "+factorial(8));   
}   
}   

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

protective orders in virginia

Keep sharing blogs like this one; they are quite good. You have given everyone in this blog access to a wealth of information.

Johnfrance

Thank you for your valuable information.

sam

Thank you for this wonderful article!

Belay Abera

This article was really helpful to me, thank you!

dfsdf

super article!