articles

Home / DeveloperSection / Articles / Method in Java

Method in Java

Manoj Pandey 2214 20-Apr-2015

A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. Think of a method as a subprogram that acts on data and often returns a value. Each method has its own name.

Java methods are where you put the operations on data (variables) in your Java code. In other words, you group Java operations (code) inside Java methods. Java methods must be located inside a Java class. We can return value or result from methods.

How many types creates method

1.      Without parameter and Return Value

class Sample
{
int value=10;
public int getValue( )
{
return value;
}
}

2.   With parameter and Return value 

 class Sample
{
public int getValue(int a, int b )
{
int c=a+b;
return c;
}
}

3.    With parameter and not return any value

class Sample
{
int v
public void getValue(int a, int b )
{
              c=a+b;
}
}

4.    Without parameter and not return any value 

class Sample
{
public void demoShow( )
{
System.out.print(“Hello Java”);
}
}
Calling method-:
1.      When method without parameter and Return Value

      int value= getValue( );

2.      When method with parameter and Return value

int sum= getValue(5,7 );
3.      When method with parameter and not return any value

        int sum= getValue( );

4.      When method without parameter and not return any value

demoShow( );


Types of method-:

1.   Static methods-: A static method is a method that can be called and executed

without creating an object. In general, static methods are used to create instance

. We can invoked (call) static method directly via class name.

For example
class Sample{
public static void sampleMethod( )
{
            System.out.print("Hello Java");}
}
class Mainclass
{
Public static void main(String args[])
{
Sample.sampleMathod();
}
}

2.      Non - static method-: When we have need object to calling method in different class.

For example
class Sample1
{
public void printMessage( )
    {
System.out.print(“ Hello Java”);
         }
}
class Sample2
{
public static void main(String args[])
{
// create object of Sample1 class for access printMessage method.
Sample1 sam1=new Sample1( );
Sam1.printMessage( );
}
}

Here I am creating a sample of Calculator with using methods in java 

class Sample {

 

     public static void main(String args[]) {

           // create object of Mathematical class

         Mathematical math = new Mathematical();

System.out.println("Addition is :  " + math.Addition(9.7, 5.3));

System.out.println("Subtraction is :  " + math.Subtraction(9.7, 5.3));

System.out.println("Multiplication is :  "

                     + math.Multiplocation(9.7, 5.3));

System.out.println("Division is :  " + math.Division(9.7, 5.3));

 

     }

 

}

 

class Mathematical {

     double result;

 

     public double Addition(double a, double b) {

           result = a + b;

           return result;

 

     }

 

     public double Subtraction(double a, double b) {

           result = a - b;

           return result;

 

     }

 

     public double Multiplocation(double a, double b) {

           result = a * b;

           return result;

 

     }

 

     public double Division(double a, double b) {

result = a / b; return result; } }           result = a / b;

           return result;

 

     }

}

Output-:

Addition is :  15.0

Subtraction is :  4.3999999999999995

Multiplication is :  51.41

Division is :  1.830188679245283

Advantages of Methods-:

·   Reusability-: After create method we can call anywhere in the class and any time in the class. We can call methods when need to result. So it’s make reusability of the code.

·    It separates the code 

·    Easy to understood.


Updated 05-Dec-2017

Leave Comment

Comments

Liked By