A java method is a series of statements that perform some repeated task. Instead of writing ten lines of code we can put those ten lines in a method and just call it in one line. It is like a shortcut.
Java methods are similar to functions or procedures in other programming languages.
on the other hand, a Java method is a set of Java statements which can be included inside a Java class.
The syntax of the java method is as follows
access-specifier return-type(data-type) method-name(arguments)
{
statement1;
statement2;//body of the function or method
statement3;
}
access-specifier will be public, private or protected
return-type is the one the data type. such as int, float,etc...
method-name is any user defined name.
arguments are the value we need to pass to the function
The example program
/**
*
* @author prabhu
*/
public class addition {
//variable declaration
int a,b,c;
public void add()
{
a=10;
b=20;
c=a+b;
System.out.println("a+b="+c);
}
public static void main(String ar[])
{
//creating object
addition add=new addition();
//calling the method in the class using the object
add.add();
}
}