Generally speaking, a method is a way to carry out a task. Similar to something like this, a Java method is a sequence of instructions that fulfills a particular mission. It provides the opportunity for code to be still used. Using methods, we can change code. The definition of Java Methods/functions, the various types of methods, method declaration, and how to invoke a method in Java are all covered in this part.
What is a java method?
A method is a sequence of instructions that fulfills a particular task. It provides the opportunity for code to be still used. It helps to make code more reusable. Once developed, a technique is applied repeatedly. You don’t have to struggle to write the code again and again. Simply adding or removing a block of code, also provides easy code modification and readability.
A class must contain a method declaration. A parenthesis is used against the name defined as method(). System.out.println()
and other pre-defined methods are available in Java, but you can also write your own custom methods to carry out certain tasks.
Example: public class Main { static void myMethod() { // code to be executed } }
Example explained
- myMethod: name is given to the method.
- static: static here means that it is part of the Main class and not an object of the Main class.
- void: void here represents the method having no return value.
How to call a method?
Write the name of the method, two parentheses (), and a semicolon (;) to call the method;
Example
In the below example, we use myMethod()
to print the text/action, when called:
public class Main { static void myMethod() { System.out.println("I just got executed!"); } public static void main(String[] args) { myMethod(); } }
The method can be called multiple times.
Example
public class Main { static void myMethod() { System.out.println("I just got executed!"); } public static void main(String[] args) { myMethod(); myMethod(); myMethod(); } }
Also Read, Learn about Java Datatypes and how we use them
Java method parameters
Paramteres and arguments
Inside the parentheses, after the method name parameters are specified. The parameters are enclosed in parentheses after the method name. Simply comma-separate as many selections as you’d want to input.
The method in the following illustration accepts a String parameter called fname
. We provide a first name when the method is called, and it uses that name inside to print the whole name.
public class Main { static void myMethod(String fname) { System.out.println(fname + " Mehta"); } public static void main(String[] args) { myMethod("Anjali"); myMethod("Tarak"); myMethod("Pankaj"); } }
A parameter is referred to as an argument when it is supplied to a method. Here in the example, fname
is a parameter, and “Anjali”, ”Tarak”, and ”Pankaj” are arguments.
Also Read, Coding Developer Seeks ‘Legal Counsel’ As Trump Social Media Appears To Violate License
Multiple Parameters
You can have multiple parameters as much as you want.
Example
public class Main { static void myMethod(String fname, int age) { System.out.println(fname + " is " + age); } public static void main(String[] args) { myMethod("Anjali", 5); myMethod("Tarak", 8); myMethod("Pankaj", 31); } }
Note: When using multiple parameters, the method call should have the same number of arguments as written in the parameters and parameters should be passed in the same order to get no errors.
Return Values
The function “void” is used here to show that the method doesn’t return value, but if you want values to be returned then primitive data types are used such as int, char, etc. To use the return keyword you should replace “void” with “return”.
Example
public class Main { static int myMethod(int x) { return 5 + x; } public static void main(String[] args) { System.out.println(myMethod(3)); } } // Outputs: 8 (5 + 3)
Also Read, Understanding Java String methods with examples
Java Method Overloading
Method overloading is the practice of having numerous methods with the same name but different parameters in a class.
The readability of the program is improved when all of the methods have the same name and we just need to do one action.
If you write the method as an (int, int) for two parameters and b(int, int, int) for three parameters, then it might be challenging for you and other programmers to understand the behavior of the method because its names are different.
Multiple methods with the same name but different parameters are possible using method overloading:
Syntax/Example
int myMethod(int b) float myMethod(float b) double myMethod(double a, double b)
Example
public class Main { static int plusMethodInt(int x, int y) { return x + y; } static double plusMethodDouble(double x, double y) { return x + y; } public static void main(String[] args) { int myNum1 = plusMethodInt(8, 5); double myNum2 = plusMethodDouble(4.3, 6.26); System.out.println("int: " + myNum1); System.out.println("double: " + myNum2); } }
It is preferable to overload one method rather than define two that should do the same function.
The plusMethod
method is overloaded in the example below to accommodate both int and double.
Example
public class Main { static int plusMethod(int x, int y) { return x + y; } static double plusMethod(double x, double y) { return x + y; } public static void main(String[] args) { int myNum1 = plusMethod(8, 5); double myNum2 = plusMethod(4.3, 6.26); System.out.println("int: " + myNum1); System.out.println("double: " + myNum2); } }
Also Read, Simple Explanation of Java Loops
Java Scope
The variables in Java are accessible only inside the region they are created, this is known as scope.
Directly declared variables are accessible anywhere in the method after the line of code where they were first defined.
public class Main { public static void main(String[] args) { // here, x cannot be used by code int x = 100; // here, x can be used by the code System.out.println(x); } }
Block Scope
public class Main { public static void main(String[] args) { // here, the code here CANNOT use x { // This is a block // here, the code here CANNOT use x int x = 100; // here, the code CAN use x System.out.println(x); } // The block ends here // here, the code here CANNOT use x } }
Also Read, Simple explanation of Java Arrays
Final Words
Given that Java is a general-purpose programming language, successful application development depends on the timely implementation of a number of functions. A method is a section of code created to carry out a certain purpose-specific function. You can read more about Java methods on this blog. A method declaration simply has to have the name, return type, a pair of parentheses, (), and a body enclosed in braces, ().
In general, method declarations consist of six parts, listed in the following order: You will learn about other modifiers, such as public and private, later. An executable component defined by a class is a method. Instance methods and class methods are the two types of methods that InterSystems IRIS offers. Invoked from a particular instance of a class, an instance method normally executes some action specific to that instance.
Leave a Reply