If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:. Report Error. Your message has been sent to W3Schools. W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.
While using W3Schools, you agree to have read and accepted our terms of use , cookie and privacy policy. In this tutorial, we will learn about Java methods, how to define methods, and how to use methods in Java programs with the help of examples. Suppose you need to create a program to create a circle and color it.
You can create two methods to solve this problem:. Dividing a complex problem into smaller chunks makes your program easy to understand and reusable. In the above example, the name of the method is adddNumbers. And, the return type is int. We will learn more about return types later in this tutorial. This is the simple syntax of declaring a method.
However, the complete syntax of declaring a method is. In the above example, we have declared a method named addNumbers. Now, to use the method, we need to call it. In the above example, we have created a method named addNumbers. The method takes two parameters a and b. Notice the line,. Here, we have called the method by passing two arguments num1 and num2. Since the method is returning some value, we have stored the value in the result variable. Note : The method is not static.
Hence, we are calling the method using the object of the class. A Java method may or may not return a value to the function call. We use the return statement to return any value. For example,.
Here, we are returning the variable sum. Since the return type of the function is int. The sum variable should be of int type. Otherwise, it will generate an error. In the above program, we have created a method named square. The method takes a number as its parameter and returns the square of the number. Here, we have mentioned the return type of the method as int. Hence, the method should always return an integer value. Note : If the method does not return any value, we use the void keyword as the return type of the method.
A new block on stack top is created when we call new method that contains specific values related to that method, like parameters and reference objects. After method execution is complete, its stack frame is cleared, the execution returns back to the calling method and the empty pace will be available for the upcoming method. This brings us to the end of the blog on Methods in Java. Remember Me! Great Learning is an ed-tech company that offers impactful and industry-relevant programs in high-growth areas.
Know More. Sign in. Log into your account. Forgot your password? Password recovery. Recover your password. Home Java Methods in Java. Fig 1. Sample Java Program with a method call. Reflection in Java.
0コメント