

{"id":7159,"date":"2018-02-03T19:59:17","date_gmt":"2018-02-03T19:59:17","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=7159"},"modified":"2026-05-16T18:11:07","modified_gmt":"2026-05-16T12:41:07","slug":"java-method","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/java-method\/","title":{"rendered":"Java Method &#8211; Declaring and Calling Method with Example"},"content":{"rendered":"<p>Imagine you have to wake up every day and perform a specific task. This task can be anything, be it brushing your teeth or playing with your dog. Consider you have to wake up every day and give milk to the street dogs. They absolutely love you, and you love them too! However, one day you fall sick and are unable to get out of bed. You ask your mom\/dad to give milk to the poor puppies outside your house. You simply tell them where you have kept the bowl and milk, and they do the same thing as you do every day!<\/p>\n<p>Your mom\/dad is now an example of a method. You pass them the information in place of method arguments, and they perform the function assigned to them by you. Java methods work exactly like this. Let us dive in!<\/p>\n<h3>Java Methods<\/h3>\n<ul>\n<li>Methods are the lines of code that perform a specific function in a program.<\/li>\n<li>Methods can either return a value or not return anything.<\/li>\n<li>The methods that do not return anything are of type void.<\/li>\n<li>The main advantage of methods in a program is code reusability.<\/li>\n<\/ul>\n<h3>Need for Methods in Java<\/h3>\n<p>Applications around the world get built for solving problems. However, when building an application, there is absolutely no need for programming each and every part of it. This is where methods come into play. If you need to implement a particular function in your application that is already programmed by someone else, you can directly implement that method in your application without worrying about that function at all.<\/p>\n<p>For example, if you are writing a function that prints all the prime numbers from 1 to 100, you can simply use a prime method and print only those numbers that yield a true boolean value when passed through this method.<\/p>\n<p>Methods simplify programming and segment blocks of specific code, which makes it easy to debug. It also enhances code readability and reusability.<\/p>\n<h3>Syntax breakdown of Java Method<\/h3>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Method-Declaration-in-Java.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84336\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Method-Declaration-in-Java.jpg\" alt=\"Methods Declaration in Java\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Method-Declaration-in-Java.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Method-Declaration-in-Java-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Method-Declaration-in-Java-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Method-Declaration-in-Java-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Method-Declaration-in-Java-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Method-Declaration-in-Java-720x377.jpg 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Method-Declaration-in-Java-520x272.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Method-Declaration-in-Java-320x167.jpg 320w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<p>The syntax of a method declaration consists of the following points:<\/p>\n<h4>1. Java Modifier<\/h4>\n<p>We learned about access modifiers in previous articles. We can specify the access of the method by modifiers. There are primarily 4 types of modifiers in Java:<\/p>\n<p><strong>a.<\/strong> public- this makes the method accessible to all classes in your application<br \/>\n<strong>b.<\/strong> private- this renders the method accessible only within the class and its subclasses.<br \/>\n<strong>c.<\/strong> protected- it makes the method accessible within the class.<br \/>\n<strong>d.<\/strong> default- this renders the method accessible within the same class and package.<\/p>\n<h4>2. Java return type<\/h4>\n<p>When the method performs the particular tasks, it generates further output at the end. The type of output that the method returns is the return type.<br \/>\nThe return type of the method can be integer, float, character, boolean, long, array, short, or byte. If the method returns nothing, then we use void.<\/p>\n<h4>3. Method name in Java<\/h4>\n<p>A method name should typically represent what its function is. It should be a verb in the lowercase. However, if the verb is more than one word, the camel case is used to write the name of the method.<br \/>\nGenerally, to add more definition, an adjective or a noun exceeds the name of the method.<br \/>\nMethod names are unique; however, to implement polymorphism, method names can be the same at times.<\/p>\n<h4>4. Java parameter list<\/h4>\n<p>Parameters are the variables that are defined during the creation of a method. Parameters store the value passed during the method call for performing the task. A method containing a list of parameters of different data types is called a parameter list.<\/p>\n<p>First brackets enclose them(). However, if there are no parameters, you must use empty parentheses.<\/p>\n<h4>5. Exception list in Java<\/h4>\n<p>This list includes the exceptions that you can expect the method to throw. There can be multiple exceptions.<\/p>\n<h4>6. Method body<\/h4>\n<p>Curly braces{} enclose this body. Upon invoking the method, the statements inside these braces execute and return a value, or no value, if it is a void function.<\/p>\n<h4>7. Method Signature in Java<\/h4>\n<p>The method name and the parameter list together is the method signature. This does not include the return type and the exceptions.<\/p>\n<p>Example Syntax of a method signature:<\/p>\n<p><strong>public static add(int x,int y)<\/strong><\/p>\n<h3>Java Static and Non-Static Methods<\/h3>\n<p>Static methods do not need objects to execute. However, there is a need for objects for accessing non-static methods.<\/p>\n<p><strong>Java program to illustrate the use of static keyword in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javamethod;\r\npublic class BasicMethod {\r\n  static void staticmethod() {\r\n    System.out.println(\"Hey! I am inside a static method. \");\r\n\r\n  }\r\n  public void nonstaticmethod() {\r\n    System.out.println(\"Hey! I am inside a non-static method. I need an object to execute myself! \");\r\n  }\r\n  public static void main(String[] args) {\r\n    staticmethod();\r\n    BasicMethod ob = new BasicMethod();\r\n    ob.nonstaticmethod();\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Hey! I am inside a static method.<br \/>\nHey! I am inside a non-static method. I need an object to execute myself!<\/div>\n<p>The object <strong>ob<\/strong> accesses the method named <strong>nonstaticmethod()<\/strong>. However, if you try to access the non-static method without an object you will end up with an error like this.<\/p>\n<p><strong>error: non-static method nonstaticmethod() cannot be referenced from a static context<\/strong><br \/>\n<strong>nonstaticmethod();<\/strong><\/p>\n<p>The static context as mentioned in the error is actually the main function. (Note that the main function is static).<\/p>\n<p><strong>The basic syntax of a method is\u00a0<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt; access modifier &gt; &lt;\r\nreturn type &gt; &lt;method name &gt; ( &lt; parameter list &gt; ) &lt; exceptions &gt; {\r\n  \/\/method body code\r\n}<\/pre>\n<h3>Calling Method in Java<\/h3>\n<p>Method calling in Java implements a stack to maintain the order of execution. The control transfers back to the code that invoked it under the following conditions<\/p>\n<p><strong>a.<\/strong> It executes all the code in the method.<br \/>\n<strong>b.<\/strong> it reaches a return statement within the code<br \/>\n<strong>c.<\/strong> It encounters an exception<\/p>\n<p><strong>Java program to illustrate the uses of methods in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javamethod;\r\npublic class BasicMethod {\r\n  public static void getminimum(int a, int b) {\r\n    if (a &lt; b) System.out.println(a + \" is the minimum of the two.\");\r\n    else if (b &lt; a) System.out.println(b + \" is the minimum of the two.\");\r\n    else System.out.println(\"both the numbers are equal\");\r\n  }\r\n  public static void main(String[] args) {\r\n    getminimum(5, 6);\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">5 is the minimum of the two.<\/div>\n<p>This program illustrates the use of a method to find out the minimum of the two arguments.<\/p>\n<h3>Java Method stack<\/h3>\n<p>Method calls in Java use a stack to monitor the method calls in a program. The method call from anywhere in the program creates a stack frame in the stack area. The local variables get the values from the parameters in this stack frame. After the completion of the program, its particular stack frame is deleted. The stack pointer points to each method execution. However, whenever a new method is called, the current method execution stops, and the stack pointer points to the new method until it finishes execution.<\/p>\n<p><strong>Java program to illustrate the method stack:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javamethod;\r\npublic class MethodStack {\r\n  static void m1() {\r\n    System.out.println(\"I am inside the m1 method and going to call the m2 method. \");\r\n    m2();\r\n    System.out.println(\"The control has returned to the m1 method. \");\r\n\r\n  }\r\n  static void m2() {\r\n    System.out.println(\"I am inside the m2 method and going to call the m3 method\");\r\n    m3();\r\n    System.out.println(\"The control has returned to m2\");\r\n\r\n  }\r\n  static void m3() {\r\n    System.out.println(\"I am inside the m3 method.\");\r\n  }\r\n  public static void main(String[] args) {\r\n    m1();\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">I am inside the m1 method and going to call the m2 method.<br \/>\nI am inside the m2 method and going to call the m3 method<br \/>\nI am inside the m3 method.<br \/>\nThe control has returned to m2<br \/>\nThe control has returned to the m1 method.<\/div>\n<h3>Passing Methods by Value in Java<\/h3>\n<p>If you have experience of programming languages, you will know that arguments to a method get passed in two ways, namely, Pass by value and pass by reference. Since Java does not have the concept of pointers, it is safe to say that Java is a strictly pass-by-value language.<br \/>\nPassing the arguments by value should follow the same order as mentioned in the method definition.<\/p>\n<p><strong>Java program to illustrate the use of pass by value:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javamethod;\r\npublic class PassByValueMethod {\r\n\r\n  static void add(int num1, int num2) {\r\n    System.out.println(\"The sum of the two numbers \" + anum1 + \" and \" + num2 + \" is \" + (num1 + num2));\r\n  }\r\n  public static void main(String[] args) {\r\n    add(5, 9);\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">The sum of the two numbers 5 and 9 is 14<\/div>\n<h3>Java Method Overloading<\/h3>\n<p>Overloaded methods are those methods that have the same name, but they differ in the type of arguments they have. These methods make the program readable. This enables the programmer to name several methods having the same names, which leads to less confusion. However, if each method needs to have a different definition, then only the arguments get changed and not the method name itself. You can easily define another method with the same name but with different arguments.<\/p>\n<p><strong>Java program to illustrate the concept of Method Overloading:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javamethod;\r\npublic class OverloadMethod {\r\n\r\n  static void add(int num1, int num2) {\r\n    System.out.println(\"The compiler understood you wanted to add two numbers of type int\");\r\n    System.out.println(\"The sum of the two numbers \" + num1 + \" and \" + num2 + \" is \" + (num1 + num2));\r\n  }\r\n  static void add(double num1, double num2) {\r\n    System.out.println(\"The compiler understood you wanted to add two  numbers of type double. \");\r\n    System.out.println(\"The sum of the two numbers \" + num1 + \" and \" + num2 + \" is \" + (num1 + num2));\r\n  }\r\n  public static void main(String[] args) {\r\n    add(5, 9);\r\n    add(6.3, 8.2);\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">The compiler understood you wanted to add two numbers of type int<br \/>\nThe sum of the two numbers 5 and 9 is 14<br \/>\nThe compiler understood you wanted to add two numbers of type double.<br \/>\nThe sum of the two numbers 6.3 and 8.2 is 14.5<\/div>\n<h3>CommandLine Argument in Java<\/h3>\n<p>In programming, certain information gets passed to a program while running it. These are the command-line arguments. They follow the program\u2019s name while it is being executed through the Command Line Interface.<br \/>\nThese arguments are accessible inside the program because Java interprets the arguments as strings.<\/p>\n<p><strong>Java program to illustrate the use of command-line args in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.datafalir.javamethod;\r\n\r\npublic class CommandLineArgs {\r\n  public static void main(String[] args) {\r\n    for (int i = 0; i &lt; args.length; i++) {\r\n      System.out.println(\"The argument number \" + i + \" is \" + args[i]);\r\n    }\r\n  }\r\n\r\n}<\/pre>\n<p>Upon executing the program in CLI like this:<br \/>\njavac CommandLineArgs.java<br \/>\njava CommandLineArgs Hey these are arguments!<\/p>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">The argument number 0 is Hey<br \/>\nThe argument number 1 is these<br \/>\nThe argument number 2 is are<br \/>\nThe argument number 3 is arguments<\/div>\n<h3>Variable Arguments in Java<\/h3>\n<p>It is not always possible to know the number of arguments that a method may need during execution. This is where the variable arguments in Java come into play. Variable arguments are represented by three consecutive dots(&#8230;). The syntax is datatype\u2026 parameterName<\/p>\n<p>However, there are certain rules to declaring variable arguments in Java:<\/p>\n<ul>\n<li>Only one var-length parameter should be present in a single definition.<\/li>\n<li>This parameter must be the last, i.e, all the regular parameters must precede it.<\/li>\n<\/ul>\n<p><strong>Java program to illustrate variable datatype in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javamethod;\r\npublic class VarArgsMethod {\r\n\r\n  static void printnum(int...numbers) {\r\n    if (numbers.length == 0) System.out.println(\"There are no numbers\");\r\n    else {\r\n      System.out.println(\"The numbers are\");\r\n      for (int i = 0; i &lt; numbers.length; i++) {\r\n        System.out.println(numbers[i]);\r\n      }\r\n    }\r\n  }\r\n\r\n  public static void main(String[] args) {\r\n    printnum(1, 2, 3, 4, 5, 21, 56, 67, 56, 5, 56, 5, 34, 63, 453, 52345);\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">The numbers are<br \/>\n1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n21<br \/>\n56<br \/>\n67<br \/>\n56<br \/>\n5<br \/>\n56<br \/>\n5<br \/>\n34<br \/>\n63<br \/>\n453<br \/>\n52345<\/div>\n<p><em><strong>Note that we did not specify any limit of numbers in the arguments of the function.<\/strong><\/em><\/p>\n<h3>Choosing the Right Literal Type in Java<\/h3>\n<p>Selecting the appropriate literal type for your data is crucial in Java programming. Using the most suitable literal type enhances code readability, maintainability, and efficiency. Here&#8217;s a quick reference to guide your decisions:<\/p>\n<ul>\n<li>Integer literals are ideal for whole numbers without decimal points.<\/li>\n<li>Floating-point literals are designed for numbers with decimal places.<\/li>\n<li>Character literals represent single characters enclosed within single quotes.<\/li>\n<li>String literals are used for sequences of characters, denoted by double quotes.<\/li>\n<\/ul>\n<h3>Java finalize Method<\/h3>\n<p>Whenever the garbage collector in Java destroys an object, it calls the finalize method. Just before the object gets killed, the finalize method executes and performs all the functions necessary.<\/p>\n<p>For example, this method can close the file when handling files using Java.<\/p>\n<p><strong>The syntax of the Java finalize method is:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">protected void finalize() {\r\n  \/\/Final functions before the garbage collector collects objects. \r\n}<\/pre>\n<p>The finalize method is of type \u201cprotected&#8221; to prevent access from outside the class. However, it is difficult to know whether the finalize method has actually worked or not. It is because the Java garbage collector may\/ may not collect the object during runtime unless the pressure in the memory is high.<\/p>\n<p><strong>Java program to illustrate the use of the finalize method:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.javamethod;\r\npublic class FinalizeMethod {\r\n\r\n  public void objcheck() {\r\n    System.out.println(\"The object is still alive. \");\r\n  }\r\n  protected void finalize() {\r\n    System.out.println(\"Code to be executed before object collection by JVM \");\r\n  }\r\n\r\n  public static void main(String[] args) {\r\n    FinalizeMethod ob = new FinalizeMethod();\r\n    ob.objcheck();\r\n    ob = null;\r\n    System.gc();\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">The object is still alive.<br \/>\nCode to be executed before object collection by the JVM<\/div>\n<p>We purposely assigned the object to a null value and called the garbage collector to invoke the finalize method.<\/p>\n<h3>Summary<\/h3>\n<p>We learned about methods in this article and how we can use them in our programs. Methods are essential to know in Java because Java code, once written, becomes reusable. Methods also segment and distribute the code effectively for easy documentation and understanding of the program.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Imagine you have to wake up every day and perform a specific task. This task can be anything, be it brushing your teeth or playing with your dog. Consider you have to wake up&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":84336,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[2276,3640,7485,7598,7601,7633,8659,11594,12933],"class_list":["post-7159","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-calling-java-method","tag-declaring-java-methods","tag-java-exception-list","tag-java-method-name","tag-java-method-signature","tag-java-parameter-list","tag-method-declaration-in-java","tag-return-type-in-java","tag-situation-of-calling-methods-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Method - Declaring and Calling Method with Example - DataFlair<\/title>\n<meta name=\"description\" content=\"A Java method is a collection of statements to perform a specific task. Learn user-defined and Standard Library Methods with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/java-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Method - Declaring and Calling Method with Example - DataFlair\" \/>\n<meta property=\"og:description\" content=\"A Java method is a collection of statements to perform a specific task. Learn user-defined and Standard Library Methods with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/java-method\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-03T19:59:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-16T12:41:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Method-Declaration-in-Java.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Method - Declaring and Calling Method with Example - DataFlair","description":"A Java method is a collection of statements to perform a specific task. Learn user-defined and Standard Library Methods with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/java-method\/","og_locale":"en_US","og_type":"article","og_title":"Java Method - Declaring and Calling Method with Example - DataFlair","og_description":"A Java method is a collection of statements to perform a specific task. Learn user-defined and Standard Library Methods with examples.","og_url":"https:\/\/data-flair.training\/blogs\/java-method\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-02-03T19:59:17+00:00","article_modified_time":"2026-05-16T12:41:07+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Method-Declaration-in-Java.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/java-method\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/java-method\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Java Method &#8211; Declaring and Calling Method with Example","datePublished":"2018-02-03T19:59:17+00:00","dateModified":"2026-05-16T12:41:07+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-method\/"},"wordCount":1781,"commentCount":9,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-method\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Method-Declaration-in-Java.jpg","keywords":["calling java method","declaring Java Methods","java exception list","java method name","java method signature","java parameter list","Method declaration in Java","return type in java","situation of Calling Methods in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/java-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/java-method\/","url":"https:\/\/data-flair.training\/blogs\/java-method\/","name":"Java Method - Declaring and Calling Method with Example - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-method\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-method\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Method-Declaration-in-Java.jpg","datePublished":"2018-02-03T19:59:17+00:00","dateModified":"2026-05-16T12:41:07+00:00","description":"A Java method is a collection of statements to perform a specific task. Learn user-defined and Standard Library Methods with examples.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/java-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/java-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/java-method\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Method-Declaration-in-Java.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Method-Declaration-in-Java.jpg","width":1200,"height":628,"caption":"Method Declaration in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/java-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Java Method &#8211; Declaring and Calling Method with Example"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=7159"}],"version-history":[{"count":20,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7159\/revisions"}],"predecessor-version":[{"id":148310,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7159\/revisions\/148310"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/84336"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=7159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=7159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=7159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}