Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Core Java Basics. No Comments on Java methods
3.3
(3)

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.

 

Introduction

 

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:


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


 

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:

void area(int s)
{
	System.out.println("Area of the square is: "+(s*s));
}

 

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:

int perimeter()
{
	return 4*side;
}

 

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:

class Square
{
	int side;
	void area(int s)
	{
		System.out.println("Area of the square is: "+(s*s));
	}
	int perimeter()
	{
		return 4*side;
	}
	int getSide()
	{
		return side;
	}
	public static void main(String[] args)
	{
		Square s1 = new Square();
		s1.side = 10;
		s1.area(s1.side); //Method call
		System.out.println("Perimeter of the square is: "+s1.perimeter()); //Method call
		System.out.println("Length of the side is: "+s1.getSide()); //Method call
	}
}

 

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.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

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 *