{"id":1084,"date":"2016-03-05T05:43:36","date_gmt":"2016-03-05T05:43:36","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=1084"},"modified":"2016-03-05T05:43:36","modified_gmt":"2016-03-05T05:43:36","slug":"method","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/method","title":{"rendered":"Method In Java With Example"},"content":{"rendered":"<p>Methods are truly the heart and soul of the java programs. A method is a self contained block of code that performs a specific task. They provide a way of defining the behavior of an object i.e. what the object does. Methods break up large and complex calculations in program that might involve many lines of code into more manageable chunks. All the execution that takes place in any application is within a method.<\/p>\n<hr \/>\n<h4><strong>Syntax of Method:<\/strong><\/h4>\n<p>Following is the syntax of method which consists of method header and method body:<\/p>\n<pre>returnType methodName(Parameter list) \u00a0\/\/Method Header\r\n\r\n{\r\n\r\n\/\/declarations and statements\r\n\r\n\/\/Method Body\r\n\r\n}<\/pre>\n<hr \/>\n<h4><strong>Parts of Method Definition<\/strong><\/h4>\n<p>Method definition consists of two parts:<\/p>\n<ul>\n<li>Method header<\/li>\n<li>Method body<strong>\u00a0<\/strong><\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong><u>Method Header:<\/u><\/strong><\/span> A method header consists of method\u2019s return type followed by the method name and optional parameter list enclosed in the parenthesis.<\/p>\n<ul>\n<li>The <strong>returnType <\/strong>in a method header specifies the type of value if any, that the method returns. If the method does not return a value then the return type must be void. For example: return type may be void, int etc.<\/li>\n<li>The <strong>methodName <\/strong>in the method header specifies the name of the method. It follows the same rules and conventions as that of identifiers. For example: method name may be area, display, show etc.<\/li>\n<li>The <strong>parameter list<\/strong> in the method header must be enclosed in parenthesis. A parameter is a variable that temporarily stores data values known as arguments that are being passed to the method whenever it is called. If no arguments are being passed to the method then the parenthesis remains empty. The parameter list takes the following form:\u00a0(datatype varName1, datatype varName2, \u2026\u2026.)<\/li>\n<\/ul>\n<ul>\n<li><strong>Examples <\/strong>of method header is:<\/li>\n<\/ul>\n<pre>int area(int l, int b)\r\n\r\nvoid show();<\/pre>\n<p><strong><span style=\"color: #ff0000;\">Important Note:<\/span> <\/strong>The parameters specified in the method header are also known as formal parameters and the arguments that are passed to the method are also known as actual parameters.<\/p>\n<p><span style=\"color: #008000;\"><strong><u>Method Body: <\/u><\/strong><\/span>The method\u2019s body enclosed in a pair of curly braces consists of local variables and constant declarations for use within the method and sequence of executable statements that takes some kind of action when the method is invoked.<\/p>\n<hr \/>\n<h4><strong>Program Example of Method<\/strong><\/h4>\n<p>Let us take an example to show the method definition in the class.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1: <\/strong><\/span>First we create a class <strong>MethodDemo<\/strong> in which we define methods:<\/p>\n<pre>class MethodDemo\r\n\r\n{\r\n\r\n  int length,breadth;\r\n\r\n  void getData(int l,int b) \/\/method definition\r\n\r\n  {\r\n\r\n    length=l;\r\n\r\n    breadth=b;\r\n\r\n  }\r\n\r\n  int area() \/\/method definition\r\n\r\n  {\r\n\r\n    int rectArea=length*breadth;\r\n\r\n    return rectArea;\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2: <\/strong><\/span>Second we create a class <strong>Rectangle<\/strong> in which we call the methods of above class:<\/p>\n<pre>class Rectangle\r\n\r\n{\r\n\r\n  public static void main(String[] args)\r\n\r\n  {\r\n\r\n    MethodDemo obj=new MethodDemo();\r\n\r\n    obj.getData(6,9);\r\n\r\n    int result=obj.area();\r\n\r\n    System.out.println(\"Area of rectangle is:\"+result);\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Area of rectangle is: 54<\/pre>\n<hr \/>\n<h4><strong>Returning a Value From Method<\/strong><\/h4>\n<p>To return a value from a method, the return statement is used. Its <strong>syntax<\/strong> is as follows:<\/p>\n<pre>return(expr);<\/pre>\n<p>Here expr is a variable, constant value or an expression.<\/p>\n<p>On execution of the return statement, the program control immediately returns to the point where the method was called. If the expression is the part of the return statement then the value of that expression is returned as the value of the method invocation.<\/p>\n<p><strong>For Example: <\/strong>Let us take a small segment from the program example of method in which we have used return statement:<\/p>\n<pre>int area()\r\n\r\n{\r\n\r\n  int rectArea=length*breadth;\r\n\r\n  return rectArea;\r\n\r\n}<\/pre>\n<p>Here this method area calculates the area of rectangle whose length and breadth is given and returns the area as an integer.<\/p>\n<p>Since we know that <strong>void<\/strong> does not return any value then you can omit the return statement or just use the keyword <strong>return <\/strong>by itself to the end of the execution of method like this:<\/p>\n<pre>return;<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> There can be more than one return statement in a method. <strong>For example<\/strong>:<\/p>\n<pre>int maximumVal(int a, int b)\r\n\r\n{\r\n\r\n  if(a&gt;b)\r\n\r\n  {\r\n\r\n    return a;\r\n\r\n  }\r\n\r\n  else\r\n\r\n  return b;\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Methods are truly the heart and soul of the java programs. A method is a self contained block of code that performs a specific task. They provide a way of defining the behavior of an object i.e. what the object does. Methods break up large and complex calculations in program that might involve many lines &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/method\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Method In Java With Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-1084","page","type-page","status-publish","hentry"],"psp_head":"<title>Method In Java With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Methods define the behavior of an object i.e. what the object does. Learn the concept in JAVA with lots of examples.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/method\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1084","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=1084"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1084\/revisions"}],"predecessor-version":[{"id":1486,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1084\/revisions\/1486"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=1084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}