{"id":5276,"date":"2021-03-23T16:43:57","date_gmt":"2021-03-23T16:43:57","guid":{"rendered":"https:\/\/ded9.com\/?p=5276"},"modified":"2026-02-09T08:24:54","modified_gmt":"2026-02-09T08:24:54","slug":"complete-training-of-java-methods-in-simple-language","status":"publish","type":"post","link":"https:\/\/ded9.com\/tr\/complete-training-of-java-methods-in-simple-language\/","title":{"rendered":"Master Java Methods: A Simple, Complete Guide"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">In this tutorial, you will learn about <a href=\"https:\/\/ded9.com\/what-is-java-programming-definition-and-java-platforms\/\">Java<\/a> methods, how to define a process, and how to use them in a program using an example.<\/span><span id=\"more-50121\"><\/span><\/p>\n<h2><span style=\"font-size: 18pt;\">What is a method?<\/span><\/h2>\n<p>In mathematics, you read about functions.\u00a0For example, f (x) = x\u00a0<sup>2 is a<\/sup>\u00a0function that returns the square value of x.<\/p>\n<blockquote>\n<p dir=\"ltr\">If x = 2, then f (2) = 4<\/p>\n<p dir=\"ltr\">If x = 3, f (3) = 9<\/p>\n<p dir=\"ltr\">and so on.<\/p>\n<\/blockquote>\n<p>Similarly, in programming, it is a block of code that performs a specific task.<\/p>\n<p>In object-oriented programming, a method is a term used for a function. Methods are limited to one class, and the behavior of a class is defined.<\/p>\n<h2>Types of Java Methods<\/h2>\n<p>The method is of two types, depending on whether it is user-defined or available in the standard library:<\/p>\n<ul>\n<li>Standard library methods<\/li>\n<li>User-defined methods<\/li>\n<\/ul>\n<h3>Standard library methods<\/h3>\n<p>Standard library methods are easy to use and internal to Java. They are provided with the Java Class Library (JCL) in the Java Archive (* .jar) with the JVM and <a href=\"https:\/\/en.wikipedia.org\/wiki\/JRE\" target=\"_blank\" rel=\"noopener\">JRE.<\/a><\/p>\n<p>For example,<\/p>\n<ul>\n<li>print () is a method from java.io.PrintStream. (&#8220;\u2026&#8221;) Print prints the string inside the quotation marks.<\/li>\n<li>Sqrt () is a method used in math class. Root returns the number.<\/li>\n<\/ul>\n<p>Consider the following example:<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">public class Numbers {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">System.out.print (\u201cSquare root of 4 is:\u201d + Math.sqrt (4));<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">Square root of 4 is: 2.0<\/p>\n<\/blockquote>\n<h2><span style=\"font-size: 18pt;\">User-defined method<\/span><\/h2>\n<p>You can also define methods within the class as you wish.\u00a0These methods are called user-defined methods.<\/p>\n<h2><span style=\"font-size: 18pt;\"><strong>How do you create a user-defined method?<\/strong><\/span><\/h2>\n<p>You must define it before using it (using the method).<\/p>\n<p>Here&#8217;s how to define methods in Java.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">public static void myMethod () {\r\n\r\nSystem.out.println (\u201cMy Function called\u201d);\r\n\r\n}<\/pre>\n<\/div>\n<p>Here is a method called myMethod () defined.<\/p>\n<h4>Before the function name, you can see the three keywords public, static, and void.<\/h4>\n<ul>\n<li>The public keyword popularizes the myMethod () method.\u00a0Public members can be accessed from outside the classroom.<\/li>\n<li>The static keyword indicates that the method can be accessed without creating an object of the class.<\/li>\n<li>The void keyword indicates that this method does not return any values.<\/li>\n<\/ul>\n<p>Our method does not accept arguments in the above program, so the parentheses are empty ().<\/p>\n<p>Structure of defining a Java method:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">modifier static returnType nameOfMethod (Parameter List) {\r\n\r\n\/\/ method body\r\n\r\n}<\/pre>\n<\/div>\n<h4>Here,<\/h4>\n<ul>\n<li>Modifier &#8211; Defines the type of access and whether this method is public, private, etc. Static &#8211; If you use the static keyword in the process, it becomes a static method. Static methods can be called from the class without creating an object.<\/li>\n<\/ul>\n<p>For example, the standard Math class&#8217;s sqrt () method is static. Therefore, we can use Math.sqrt directly without creating an object for the Math class.<\/p>\n<ul>\n<li>ReturnType &#8211; A method can return a value.<\/li>\n<\/ul>\n<p>Which can return local data types (int, float, double, etc.), local objects (string, map, list, etc.), or any internal and user-defined object.<\/p>\n<p>If the method does not return a value, its return type is void.<\/p>\n<ul>\n<li>nameOfMethod &#8211; The method name is an identifier.<\/li>\n<\/ul>\n<p>You can name the method anything. However, it is more common to name it based on the tasks it performs\u2014calculateInterest, calculateArea, and so on.<\/p>\n<ul>\n<li>Parameters (arguments &#8211; Parameters are values \u200b\u200bpassed to the method. You can pass any number of arguments using the process.<\/li>\n<li>Method body &#8211; Specifies what the method does, how parameters are manipulated by programming commands, and what values \u200b\u200bare returned. The code inside} is the body of the process.<\/li>\n<\/ul>\n<h2><span style=\"font-size: 18pt;\"><strong>How to call the Java method?<\/strong><\/span><\/h2>\n<p>Now that you have defined the method, you must use it. To do this, you must call the method:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">myMethod ();<\/pre>\n<\/div>\n<p>This command invokes the myMethod () method that was previously defined.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"lazyloaded aligncenter wp-image-50123\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2025\/02\/c-users-mr-desktop-calling-function-java-jpg.jpeg\" alt=\"This command invokes the myMethod () method that was previously defined.\" width=\"486\" height=\"266\" data-ll-status=\"loaded\" \/><\/p>\n<p>1. While Java executes the program code, it encounters myMethod ().\u00a0(In code)<\/p>\n<p>2. The execution then goes to the myFunction () method and executes the code inside the method&#8217;s body.<\/p>\n<p>3- After executing the code inside the method&#8217;s body, the program returns to the central state and executes the following expression.<\/p>\n<p><strong>Example: Java method program<\/strong><\/p>\n<p>Let&#8217;s look at a Java method by defining a Java class.<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Main {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cAbout to encounter a method.\u201d);<\/li>\n<li dir=\"ltr\">\/\/ method call<\/li>\n<li dir=\"ltr\">myMethod ();<\/li>\n<li dir=\"ltr\">System.out.println (\u201cMethod was executed successfully!\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">\/\/ method definition<\/li>\n<li dir=\"ltr\">private static void myMethod () {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cPrinting from inside myMethod ()!\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h4>When you run the program, the output will be equal to:<\/h4>\n<blockquote>\n<p dir=\"ltr\">About to encounter a method.<\/p>\n<p dir=\"ltr\">Printing from inside myMethod ().<\/p>\n<p dir=\"ltr\">Method was executed successfully!<\/p>\n<\/blockquote>\n<p>The myMethod () method in the above program does not accept any arguments.\u00a0Also, this method does not return any values \u200b\u200b(the return type is void).<\/p>\n<p>Note that we have named this method without creating a class object. This was possible because myMethod () is static.<\/p>\n<h4>In the example below, our method is non-static and is inside another class.<\/h4>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Main {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">Output obj = new Output ();<\/li>\n<li dir=\"ltr\">System.out.println (\u201cAbout to encounter a method.\u201d);<\/li>\n<li dir=\"ltr\">\/\/ calling myMethod () of Output class<\/li>\n<li dir=\"ltr\">obj.myMethod ();<\/li>\n<li dir=\"ltr\">System.out.println (\u201cMethod was executed successfully!\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Output {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">\/\/ public: this method can be called from outside the class<\/li>\n<li dir=\"ltr\">public void myMethod () {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cPrinting from inside myMethod ().\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h4>When you run the program, the output will be equal to:<\/h4>\n<blockquote>\n<p dir=\"ltr\">About to encounter a method.<\/p>\n<p dir=\"ltr\">Printing from inside myMethod ().<\/p>\n<p dir=\"ltr\">Method was executed successfully!<\/p>\n<\/blockquote>\n<p>Note that we first created an object for class output. Because myMethod () is a non-static method.<\/p>\n<h2><span style=\"font-size: 18pt;\">Java methods with received arguments and return values<\/span><\/h2>\n<p>A Java method can have zero or more received arguments and may return a value.<\/p>\n<p><strong>Example: The return value of a method<\/strong><\/p>\n<p>Let&#8217;s give an example and return some value from the method.<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class SquareMain {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">int result;<\/li>\n<li dir=\"ltr\">result = square ();<\/li>\n<li dir=\"ltr\">System.out.println (\u201cSquared value of 10 is:\u201d + result);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public static int square () {<\/li>\n<li dir=\"ltr\">\/\/ return statement<\/li>\n<li dir=\"ltr\">return 10 * 10;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h4>Output<\/h4>\n<blockquote>\n<p dir=\"ltr\">Squared value of 10 is: 100<\/p>\n<\/blockquote>\n<p>In the above code snippet, the square () method does not accept arguments and always returns a square value of 10.<\/p>\n<p>Note that the recursive type (square) is an integer. This means that the method returns the value of an integer.<\/p>\n<p><img decoding=\"async\" class=\"lazyloaded aligncenter wp-image-50124\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2025\/02\/c-users-mr-desktop-returning-value-from-method-jp.jpeg\" alt=\"As you can see, the scope of this method is limited because it always returns the same value.\" width=\"387\" height=\"259\" data-ll-status=\"loaded\" \/><\/p>\n<p>As you can see, the scope of this method is limited because it always returns the same value.<\/p>\n<p>Now, let&#8217;s modify the above code snippet so that instead of always returning the square value of 10, it returns the square value of each integer passed to the method.<\/p>\n<h4><strong>Example: Method with received arguments and return value<\/strong><\/h4>\n<blockquote>\n<ol>\n<li dir=\"ltr\">public class SquareMain {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">int result, n;<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">n = 3<\/li>\n<li dir=\"ltr\">result = square (n);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cSquare of 3 is:\u201d + result);<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">n = 4<\/li>\n<li dir=\"ltr\">result = square (n);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cSquare of 4 is:\u201d + result);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">static int square (int i) {<\/li>\n<li dir=\"ltr\">return i * i;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h4>Output<\/h4>\n<blockquote>\n<p dir=\"ltr\">Squared value of 3 is: 9<\/p>\n<p dir=\"ltr\">Squared value of 4 is: 16<\/p>\n<\/blockquote>\n<p>Now, the square () method returns the square value of each integer passed to it.<\/p>\n<p><img decoding=\"async\" class=\"lazyloaded aligncenter wp-image-50125\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2025\/02\/c-users-mr-desktop-passing-arguments-returning-va.jpeg\" alt=\"Now, the square () method returns the square value of each integer passed to it.\" width=\"387\" height=\"284\" data-ll-status=\"loaded\" \/><\/p>\n<p>Java is an entirely dependent language. The compiler will get an error if you use any data type other than int (in the example above).<\/p>\n<p>The n argument in the getSquare () method is called an actual argument when calling a method.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">result = getSquare (n);<\/pre>\n<\/div>\n<p>The i-parameter accepts the arguments passed in the getSquare (int i) method definition, which is called the formal argument. The formal argument&#8217;s type must be explicitly typed.<\/p>\n<h4>You can use a comma to send multiple arguments to a Java method. For example,<\/h4>\n<blockquote>\n<ol>\n<li dir=\"ltr\">public class ArithematicMain {<\/li>\n<li dir=\"ltr\">public static int getIntegerSum (int i, int j) {<\/li>\n<li dir=\"ltr\">return i + j;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public static int multiplyInteger (int x, int y) {<\/li>\n<li dir=\"ltr\">return x * y;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">System.out.println (\u201c10 + 20 =\u201d + getIntegerSum (10, 20));<\/li>\n<li dir=\"ltr\">System.out.println (\u201c20 x 40 =\u201d + multiplyInteger (20, 40));<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h4>Output<\/h4>\n<blockquote>\n<p dir=\"ltr\">10 + 20 = 30<\/p>\n<p dir=\"ltr\">20 x 40 = 800<\/p>\n<\/blockquote>\n<p>The data types of the actual and formal arguments must match, i.e., the data type of the first actual argument must match the first formal argument. Likewise, the type of the second actual argument must match the type of the second formal argument, and so on.<\/p>\n<p><strong>Example: Calculate the squares of the numbers 1 to 5<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">public class JMethods {<\/li>\n<li dir=\"ltr\">\/\/ method defined<\/li>\n<li dir=\"ltr\">private static int getSquare (int x) {<\/li>\n<li dir=\"ltr\">return x * x;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">for (int i = 1; i &lt;= 5; i ++) {<\/li>\n<li dir=\"ltr\">\/\/ method call<\/li>\n<li dir=\"ltr\">result = getSquare (i)<\/li>\n<li dir=\"ltr\">System.out.println (\u201cSquare of\u201d + i + \u201dis:\u201d + result);\u00a0}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h4>Output<\/h4>\n<blockquote><p>Square of 1 is: 1<\/p>\n<p>Square of 2 is: 4<\/p>\n<p>Square of 3 is: 9<\/p>\n<p>Square of 4 is: 16<\/p>\n<p>Square of 5 is: 25<\/p><\/blockquote>\n<p>The getSquare () method considers int a parameter in the above code snippet. Based on the argument submitted, the process returns its square value. The i argument of type int is passed to the getSquare () method during the method call.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">result = getSquare (i);<\/pre>\n<\/div>\n<p>The x parameter accepts the submitted argument.\u00a0[In function definition (getSquare (int x)]<\/p>\n<p>return i * i;: Is the return command that returns the value by calling a method and terminates the function.<\/p>\n<p><strong>Did you notice we used the getSquare method 5 times?<\/strong><\/p>\n<h2><span style=\"font-size: 18pt;\">What are the advantages of using these methods?<\/span><\/h2>\n<ul>\n<li>The main advantage is the ability to reuse the code. You can write a method once and use it several times, not having to rewrite the entire code each time. Think about &#8220;write once, reuse several times.&#8221;<\/li>\n<li>Methods make code more readable. For example, the getSalaryInformation () method is more legible than reading lines of code to learn what this method will do.<\/li>\n<\/ul>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is a Java method?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>A method is a block of code inside a class that performs a specific task (similar to a function).<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do you pass arguments and return values in Java methods?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>You list parameters in parentheses when defining the method, and use a return statement to send back a value (unless the method is void).<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Why should you use methods in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Methods help you reuse code, make programs more readable, and break down complex tasks into manageable parts.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about Java methods, how to define a process, and how to use them in a program using an example. What is a method? In mathematics, you read about functions.\u00a0For example, f (x) = x\u00a02 is a\u00a0function that returns the square value of x. If x = 2, then f [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":5277,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[840],"class_list":["post-5276","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","tag-java"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/comments?post=5276"}],"version-history":[{"count":8,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5276\/revisions"}],"predecessor-version":[{"id":266998,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5276\/revisions\/266998"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media\/5277"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media?parent=5276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/categories?post=5276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/tags?post=5276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}