---
title: "Method in Java"  
description: "In this article I am telling about Method in java"  
author: "Manoj Pandey"  
published: 2015-04-20  
updated: 2017-12-05  
canonical: https://www.mindstick.com/articles/1734/method-in-java  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# Method in Java

A method is a set of code which is referred to by name and can be called (invoked) at any point in a [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) simply by utilizing the method's name. Think of a [method as](https://www.mindstick.com/forum/159054/how-to-mark-a-method-as-obsolete-or-deprecated-in-c-sharp) a subprogram that acts on data and often returns a value. Each method has its own name.\

Java [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best) are where you put the [operations](https://www.mindstick.com/news/2675/bmw-agrees-to-integrate-blockchain-with-operations-partners-with-bnb-chain-coinweb) on data ([variables](https://www.mindstick.com/articles/715/php-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](https://www.mindstick.com/forum/33675/how-to-convert-return-value-of-abmulivaluecopylabelatindex-into-nsstring-in-ios) or result from methods.

##### How many types creates method

1. Without [parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp) 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 vpublic 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](https://www.mindstick.com/forum/216/can-i-override-a-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](https://www.mindstick.com/articles/38/calculator-in-asp-dot-net-using-javascript) 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.

---

Original Source: https://www.mindstick.com/articles/1734/method-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
