Method

Method

Method is a collection of statements used to perform specific operation.
syntax:
accessmodifier returntype methodname(list of parameters/no parameters)
{
..
..
..
}
In the above syntax access modifers are used to change the current properties of method or constructor.In java language it is optional.

returntype:

These are classified into two types.
  1. Noreturn type (means void)
  2. Return type(means any primitive data type)
Method name any userdefined name.
In java language these methods are classisfied into following types
  1. Noparametarized method
  2. Parametarized Method
  • Noparametarized method:
  • If any method signature does not contain parameters is known as Noparametarized method
    syntax:
    accessmodifier returntype methodname()
    {
    ..
    ..
    ..
    }
    
  • Parametarized Method
  • If any method signature contains List of parameters is known as parametarized method
    syntax:
    accessmodifier returntype methodname(datatype variable1,datatype variable2,....)
    {
    ..
    ..
    ..
    }
    
Based on the return type these methods are classified into following types
  1. No return type with no Parametarized
  2. No return type with Parametarized
  3. return type with no Parametarized
  4. return type with Parametarized

1.No return type with no paramitarized


import java.util.*;
class Factorial
{
	void fact()
	{
		int x,f=1,n;
		Scanner s=new Scanner(System.in);
		System.out.println("Enter n Value");
		n=s.nextInt();
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		System.out.println(f);
	}
}

public class Rain {

	public static void main(String[] args)  {
	
		Factorial f=new Factorial();
		f.fact();
	}
	
	}

2.No return type with paramitarized


import java.util.*;
class Factorial
{
	void fact(int n)
	{
		int x,f=1;
		
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		System.out.println(f);
	}
}

public class Rain {

	public static void main(String[] args)  {
	
		Factorial f=new Factorial();
		int n;
		Scanner s=new Scanner(System.in);
		System.out.println("Enter n Value");
		n=s.nextInt();
		f.fact(n);
	}
	
	}

3.return type with no paramitarized


import java.util.*;
class Factorial
{
	int fact()
	{
		int x,f=1,n;
		Scanner s=new Scanner(System.in);
		System.out.println("Enter n Value");
		n=s.nextInt();
		
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		return f;
	}
}

public class Rain {

	public static void main(String[] args)  {
	
		Factorial f=new Factorial();
		int a=f.fact();
		System.out.print(a);
	}
	
	}

4.return type with paramitarized


import java.util.*;
class Factorial
{
	int fact(int n)
	{
		int x,f=1;
		
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		return f;
	}
}

public class Rain {

	public static void main(String[] args)  {
	
		Factorial f=new Factorial();
		int n;
		Scanner s=new Scanner(System.in);
		System.out.println("Enter n Value");
		n=s.nextInt();
		int a=f.fact(n);
		System.out.print(a);
	}
	
	}
For more prorams click here