# METHODS

&#x20;**Definition:** A Java Method is a collection of statements that are grouped together to perform an operation and executed whenever called or invoked.&#x20;

#### (1)Why methods

#### (2) Re-Usability

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfHtMBy97e5A1gauKg%2F-LTfIKWp8vg2u-yGOKLK%2Fimage.png?alt=media\&token=ccadfe54-656d-4ebd-adf4-7c98359fec48)

&#x20;**Access-level:** public, protected, default, private.

&#x20;**Modifier:** static or non-static.&#x20;

**Return-type:** Primitive data-type or Derived data-type.

&#x20;**Method-name:** Is an identifier can be any valid identifier.

&#x20;**arguments:** inputs for a method.

> &#x20;**Note:** Return type and return value should match.

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfHtMBy97e5A1gauKg%2F-LTfIez6fBy5zS_pUIcs%2Fimage.png?alt=media\&token=4b9a96a1-ff80-4ccc-a28f-d300cef94f3c)

#### &#x20;Part-1:&#x20;

The following process takes place when we execute a program:

&#x20;**1)** Program execution happens in RAM.

&#x20;**2)** RAM memory is divided into 2 types,

&#x20;**a)** Stack: For execution purpose.&#x20;

**b)** Heap: For Storage purpose.&#x20;

**3)** JVM will be loaded onto stack automatically.

&#x20;**4)** JVM will call class loader (Built-in Library).

&#x20;**5)** Class Loader will create a static pool in the heap memory and all the static members of the class will be loaded on to it.

&#x20;Static pool name will be similar to class name.

#### &#x20;Part-2:

&#x20;**1)** JVM will check the availability of main method in the static pool, if its available, JVM will load the main method for execution purpose.

**2)** JVM will pass the control to main method so that main method can execute its statement.

#### &#x20;Part-3:&#x20;

**1)** From main method, some method is called by using static pool name i.e., Classname.someMethod(1);

&#x20;**2)** someMethod() will be loaded onto the stack.&#x20;

**3)** Control will pass to the someMethod(), so it can executes its statement.&#x20;

**4)** someMethod() after its execution will return the control back to main method.

&#x20;**5)** someMethod() will be erased from the stack.

&#x20;**6)** Main method resume its execution and finally it will return the control back to JVM.

&#x20;**7)** Main method will be erased from stack.

&#x20;**8)** JVM will call Garbage Collector(Daemon Thread).&#x20;

**9)** Garbage Collector will clear the contents from the HEAP memory.

&#x20;**10)** JVM will exit from the stack.&#x20;

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfHtMBy97e5A1gauKg%2F-LTfJoNL2UN_WMqUSpol%2Fimage.png?alt=media\&token=439999bd-0cde-484a-9bcc-cb6adf288a1e)

#### &#x20;Output: 4

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfHtMBy97e5A1gauKg%2F-LTfK5cYUSmX_P57GthI%2Fimage.png?alt=media\&token=e335ea2b-a56c-4b33-a6eb-2f267c78a4a8)

&#x20;**Output:** Result is: 27

&#x20;**Note:**&#x20;

**1)** void means return no value but return control.

&#x20;**2)** If the method return-type is void, then that method cannot return any value after processing.

&#x20;**3)** If any methods return-type is void, then developing return statement is not mandatory.

&#x20;**4)** If any methods return-type is void and if the programmer has not developed to return statement then compiler will develop return statement automatically at compile time.&#x20;

**5)** Developing methods with arguments is not mandatory i.e., we can develop methods without arguments.&#x20;

**6)** The methods without arguments cannot receive any input at the time of method invocation.

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfHtMBy97e5A1gauKg%2F-LTfKf5c7TsJJla2atOn%2Fimage.png?alt=media\&token=59cd266e-6854-4cc9-917a-0f02d537bc59)

#### &#x20;Output:

&#x20;CTE Return type required

> &#x20;Note: Can we develop a method without return type? No, we cannot develop any method without return type, return type is mandatory should be either a primitive data type or Derived Data-type or void, but arguments for a method is not mandatory

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfHtMBy97e5A1gauKg%2F-LTfKxOBDC4dCkfvppZw%2Fimage.png?alt=media\&token=1357a81f-e079-4789-a4fc-e5fa3c04495e)

#### &#x20;Output:

&#x20;Result is :81

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfHtMBy97e5A1gauKg%2F-LTfL3mlsCD77lf4kURo%2Fimage.png?alt=media\&token=5cbdf23f-b6d4-4749-b400-8094bdcac2af)

#### &#x20;Output:&#x20;

Result is :90

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfHtMBy97e5A1gauKg%2F-LTfLAtcx3oqXyJnw_Hz%2Fimage.png?alt=media\&token=4d1d94e0-562f-405a-9a3d-1cf38207f953)

#### &#x20;Output:

&#x20;90.5

> &#x20;**Note:** In System.out.println we cannot call the method of type void, which cannot return value. We can call only non-void method.

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfHtMBy97e5A1gauKg%2F-LTfLOe9-WHE0JhKKiLF%2Fimage.png?alt=media\&token=bc9bc128-850c-4093-9bcd-328a36d786e9)

#### &#x20;Output:&#x20;

Erroneous tree type:

> &#x20;**Note:** If the methods Return type is void, we cannot call it in “System.out.println” or having the “System.out.println” we can only call those methods which will return some value (non-void method). When we opt for void method? Whenever we are not expecting any return value from the method after processing then we should develop those methods with return type. When we opt for non-void method? Whenever we are expecting the return value from the method and based on that return www.testingbar.com Sujoy@testingbar.com value if we want to do further processing then we should choose non-void method.

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfHtMBy97e5A1gauKg%2F-LTfLlC2UcL-9ToRsfib%2Fimage.png?alt=media\&token=2745c83f-b9ab-4fbd-a1bd-91594e8969d0)

#### &#x20;Output:

&#x20;**(1)**&#x52;esult is true

&#x20;**(2)**&#x52;esult is false

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfHtMBy97e5A1gauKg%2F-LTfM9eZM0HTUotTGMne%2Fimage.png?alt=media\&token=11e87998-864a-4b77-b472-9bbfe4a72850)

#### &#x20;Output:&#x20;

Result is :30

&#x20;Result is :30&#x20;

Result is :40

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfHtMBy97e5A1gauKg%2F-LTfMtUNNrTOKKXtMtGc%2Fimage.png?alt=media\&token=a711e377-bba3-4877-a90e-6c9b9eab9ab8)

#### &#x20;Output:

&#x20;Result is:true

&#x20;Result is:true

&#x20;Result is:false

&#x20;Result is:false
