×

Methods in Java

The Methods in Java are the collection of statements that are executed when the method is called. By using the methods, the complexity of writing the code decreases.

The method consists of the block of code to perform a task. The methods are used for the Reusability feature i.e., We can be able to call the methods for number of times according to our problem instead of writing the same code for a greater number of times.

The main() method in Java is the most important method as the execution of a Java program start with the main method.

Declaration of Methods in Java

The Method declaration or the Method Signature has the following components

  • Return data type
  • Name of Method
  • Parameters

We can also include the access modifiers, if not mentioned it takes the “Default access modifier/ specifier”.

Syntax:

Access_modifer return_datatype  Method_Name(Parameters)

Example:

public int Matrix (int a , int b)

Calling a Method in Java

The method is executed only when the method is called. The basic way of calling method is, writing the method name then parenthesis i.e. ().

Example:

import java.io.*;
class MethodDemo
{
static void Method()
{
System.out.println(“Method Invoked”);
}
public static void main (String args[])
{
Method();
}
}   

Output:

Method Invoked

The method which doesn’t return any value is declared by the void data type.

Passing parameters in Methods

In the process of Calling a method, we can give the parameters inside the parenthesis ().

//Example program for the Parameterized Method
import java.io.*;
class MethodDemo
{
static void method(int a)
{
System.out.println(“The Number is”+a);
} 
public static void main(String args[])
{
method(10);
}
} 

Output:

The Number is 10

Types of Methods

There are two types of Methods they are,

  • Predefined Methods.
  • User- defined Methods.

Predefined Methods:

The predefined methods in Java are the methods which are prewritten in the Java libraries. It is also called as built-in Method. Examples of Predefined methods are length() , sqrt(), equals() etc.

Example:

class PreMethod
{
public static void main(String args[])
{
System.out.println(“Max Number is ”+Math.max(9,7));
}
}

Output:

Max Number is 9  

User-defined Methods

The user-defined methods are the methods which are written by the user/programmer.

Creation of User-defined method

Example Program for User defined method checking a person is eligible for vote or not.

Code:

class MDemo
{
public static void Elec(int x)
{
if(x>18)
{
System.out.println(“Eligible”);
}
else
{
System.out.println(“Not- Eligible”);
}
}  
// calling the method in main method
public static void main(String args[])
{
Elec(20); // calling method 
}
}  

Output:

Eligible

The above program consists of method Elec which is used to check the eligibility for casting the vote or not. The Elec method is being called in the Main method with parameters “x”.

Method Overloading

If the class consists of multiple methods with the same name, then it is called “Method Overloading”.

Example:

public class Ex
{
public static void main(String args[])
{
int A =10;
int B=12;
double d1 = 10.25;
double d2 = 18.50;
Max(A,B);
Max(d1,d2);
}
public static void Max(int x , int y)
{
if(x>y)
{
System.out.println(“Maximum Number : ”+x);
}
else
{
System.out.println(“Maximum Number : ”+y);
}
}
public static void Max(double i, double j)
{
if(i>j)
{
System.out.println(“Maximum Number : ”+i);
}
else
{
System.out.println(“Maximum Number : ”+j);
}
}


} 

Output:

Maximum Number: 12
Maximum Number: 18.50

In the example, we have created the two methods with the same name “Max” and the parameters of the two methods are different i.e., the first method parameters are given as integer datatype and second method parameters are given as double datatype. The program executes perfectly without any errors.

Abstract Methods

The abstraction is the process of hiding the unnecessary information or code to the user. To achieve the abstraction, we use the abstract methods.

For Example, if the user doesn’t want to see any information about the code and all the clumsy stuff in the code so we use the abstraction to show the relevant details to the user which are useful to them.

Creating the Abstract Methods

The Abstract methods are declared by using the “abstract” keyword before creating a method.

The Abstract class can consist of both Abstract method and Concreate methods. The Concrete methods are the just the regular methods.

The Abstract methods doesn’t create any new object, but they can be able to create the references.

The Abstraction is done by the concept of using the inheritance.

Example:

abstract class Dem
{
public abstract void connect();
public void check()
{
System.out.println(“Checking Connections”);
}
} 
class Be extends Dem
{
public void connect()
{
System.out.println(“Bluetooth Connected”);
}
}
class Mainn 
{
public static void main(String args[])
{
Be b = new Be();
b.check();
b.connect();
}
}

Output:

Checking Connections
Bluetooth Connected

Summary

The methods in Java are the collection of statements which are enclosed in the block i.e., {} are executed by the call of the method. There are two types of methods they are, Predefined methods and User defined methods. In the predefined methods the methods are already prewritten the Java libraries. The User defined methods are the methods which are written by the user or the Programmer. There is a special type of method that is abstract method, the abstract method is used to achieve the abstraction in the program.


Related Topics

Interface Program in Java

Interface Program in Java In the previous topic, we discussed that abstraction is possible through the interface and abstract class. An abstract class provides partial to 100% abstraction. 100 %abstraction in...

4 minutes read.

Java ArraylistRemove() Time Complexity

In this tutorial, we will learn about how we can remove time complexity in Java ArrayList. But unless we will not learn what is ArrayList, we don’t understand this process....

7 minutes read.

Java Binary Tree

The non-linear data structure known as a binary tree is a type of tree, and because it stores data in a hierarchical manner, it is mostly utilised for finding and...

7 minutes read.

Inheritance Program in Java

Inheritance Program in Java Inheritance is one of the important pillars of Object-Oriented Programming that facilitates parent-child relationships in programming. Using inheritance, we can create a new class with the help...

7 minutes read.

Mutable class in Java

A language for object-oriented programming is Java. Because this is an object-oriented language of programming, all of its mechanisms and methods are based on objects. Java has a concept of...

6 minutes read.

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String. Using valueOf(boolean) method Using toString(boolean) method For both the above methods, if the passes Boolean...

2 minutes read.

Upcasting in Java

To know what is Upcasting in Java we should first equip ourselves about what is casting or type casting in Java. Casting The process of providing or replacing a reference variable of...

3 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

String Declaration in Java

A string is a group of characters. In Java, the string can be treated as both class and datatype. In Java programming, the String class have many advantages. Everything in...

3 minutes read.

if-else Program in Java

if-else Program in Java The if-else program in Java controls which code snippet will execute. The if-else program is very basic and yet very important. Because it checks how well one...

19 minutes read.

Java String Matches vs Contains

String Matches in Java The matches() function and its variations are used to determine whether or not a provided text matches a regular expression. The functioning as well as output of...

3 minutes read.

Automatic Resource Management

In computer programming, resource management refers to the wide set of techniques for the effective management of the system resources. There are two ways of management of resources: The computer program itself...

2 minutes read.

Display List of TimeZone with GMT and UTC in Java

It is vital to establish the right TimeZone in Java code when working with dates for Daylight Saving Time. In this part, we will present the time zones with GMT. TimeZone Those...

5 minutes read.

Catalan number in Java

In general mathematics, Catalan numbers can be defined as the sequence of natural numbers that frequently occur in counting problems often encountered in recursively defined objects. Mathematical formula of Catalan number Coming...

3 minutes read.

Dictionary in Java

Dictionary in Java The Dictionary class represents a key-value relation which maps keys to values. In Dictionary class, every key and every value is an object. It is an abstract class associated with...

2 minutes read.

How to Convert Octal to Decimal in Java

How to Convert Octal to Decimal in Java There are two methods to convert Octal to Decimal: Using parseInt() method Using user-defined logic Using Integer.parseInt() method The Integer.parseInt() method is a static method...

2 minutes read.

How to Develop Programming Logic in Java?

Introduction In the world of software development, Java programming language is one of the most powerful programming languages that is used to create a wide range of applications. It includes desktop,...

18 minutes read.

Insertion Sort in Java

Insertion Sort in Java Insertion sort in Java is a simple sorting algorithm that works in the same way as we hold cards in hand. Insertion sort does the sorting element-by-element,...

3 minutes read.

Diffie Hellman Algorithm in Java

In this section, you will be acknowledged about Diffie Hellman algorithm clearly step wise along with an example and also an example program. Diffie Hellman Algorithm One of the most significant algorithms...

3 minutes read.

How to Call a Method in Java

In Java, a method is a collection of statements that perform a specific task or action.It can accept data with the help ofitsarguments. It  is also called a function. In order...

9 minutes read.