Here during this article you may be get better idea regarding how methods are very essential components of any class in java or we can say in each language that supports object oriented programming language.It’s better to understand about method’s conception and alternative types before executing it’s programs.So initial question that you simply got to recognise that what is method?

The word method itself recommend the exact mean,The method is defined as the function that is designed for performing special kind of operation using provided value and defined type of it’s returning value.Java offers them so much power and flexibility.I think you all understand it very well.So lets keep in mind the syntax form.










return_type method_name(parameter-list)


return-type defines the value that method can return a the end of its execution.Method_name is the name of method that depends on you and parameter-list is the list of arguments that you need to use in your code.Note one thing that return_type and parameter_list should have same data type.The word void means nothing.If function have void return type then we can say it returns nothing.Depending upon return_type and parameter_list we can divide it in four parts.Here we have divided it in three 
parts mainly .

Methods in java

1.Method with no return type and no arguments

The first one is here.As from name you’ll guess that it have no return type and haven’t any parameters in argument section.The syntax of it’s like below code



void name(void);


If you wish do additional idea you want it then you’ll execute below simple example.


Source code:–


class rectangle{
int length;
int width;
void calarea()
{
System.out.println(“Area of rectangle is :” +(length*width));
}
}
class method1{
public static void main(String args[])
{


rectangle r=new rectangle();

r.length=10;

r.width=20;
r.calarea();
}
}

output:–


Area of rectangle is:200


2.Method with return sort



As the name suggest,it have return type but does not have parameters.So after we use it in main class we have to store the returning value first of all.


syntax

return_type name(void)

Simple example is there below..


source code


class sum{
int a;
int b;
int cal()
{
return a+b;
}
}
class method2{
public static void main(String args[]){
int c;
sum s=new sum();
s.a=10;
s.b=20;
c=s.cal();
System.out.println(“The value of sum = “+c);
}
}
output:
The value of sum =30

3.Method with return type and arguments list



It contains both return type as well as parameter list.So we have to pass the parameters in function body so have to store its returning value mainly.
Syntax
return_type name(arguments)

Source code


import java.util.Scanner;

class sq{
int a;
int cal(int a)
{
return a*a;
}
}
class method3{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
System.out.println(“Enter number”);
sq e=new sq();
int k=s.nextInt();
int d;
d=e.cal(k);
System.out.println(“Square is =”+d);
}
}
output:


Enter number
4
Square is 16

Logic of all above code:In first simple program we have initialised rectangle’s variables with using object in main class and then with help of that object we called the method within the other class.In second example we have defined the return type integer and initialise both value and stored inside other variable of its sum.In third example we passed the arguments to the function of other class and calculated square using that value.If you find any difficulty(I know its not possible) then comment below  and don’t forget to like our fb page.Happy coding!!

By jigar

Leave a Reply

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