Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

September 30, 2014 Categories: Core Java Basics. No Comments on Java methods

In this article you will learn about Java methods. You will look at what is a method, how to create methods in a Java class, how to call a method, how to return a value from a method and more.

Method: A method is a piece of code to solve a particular task. A method is analogous to functions in C and C++. Methods are defined inside a class. The syntax for creating a method is as shown below:

Java methods syntax

 

The return_type specifies the type of value that will be returned by the method. The name of the method is specified by method_name. Every parameter in the parameters list follows the below syntax:

datatype parameter_name

The parameters count can be zero or more based upon your requirements. The body of the method is represented using the braces { and }. Body of the method is also known as the method definition

If a method does not return any value, its return type must be void. A method can return a single value back by using the return statement. Syntax for using return statement is as shown below:

return value;

As an example, let’s create a method which takes side of a square as parameter and prints out the area of the square. The method is shown below:

In the above method, s is a parameter and the return of the method is void as the method is not returning back any value.

Let’s create another method which returns back the perimeter of the square. The method is shown below:

In the above method, there are no parameters. The variable side is an instance variable of the class (see below). The method perimeter returns back an integer value using the return keyword. So the return type of the method is int.

A method in a class can be called by creating an object to that class and then use the dot operator followed by the method name and arguments if any. Syntax for a method call is as shown below:

object_reference.method_name(arguments);

Now, let’s look at the entire Java program which contains the class Sqaure along with the above methods for computing area, perimeter and some other code:

Output for the above program is:

Area of the square is: 100
Perimeter of the square is: 40
Length of the side is: 10

A well written Java program is one which contains a set of classes that hides their fields (instance variables) from direct access and allows them to be accessed only through methods.

The values passed in a method call are known as arguments and the variables declared in the method to receive the values from the method call are known as parameters.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me