|
| 1 | +# Java Tutorial: Methods in Java |
| 2 | +- Sometimes our program grows in size, and we want to separate the logic of the main method from the other methods. |
| 3 | +- For instance, if we calculate the average of a number pair 5 times, we can use methods to avoid repeating the logic. [DRY – Don’t Repeat Yourself] |
| 4 | + |
| 5 | +### Syntax of a Method |
| 6 | +- A method is a function written inside a class. Since Java is an object-oriented language, we need to write the method inside some class. |
| 7 | +- Syntax of a method : |
| 8 | +``` |
| 9 | +returnType nameOfMethod() { |
| 10 | +//Method body |
| 11 | +} |
| 12 | +``` |
| 13 | +- The following method returns the sum of two numbers |
| 14 | +``` |
| 15 | +int mySum(int a, int b) { |
| 16 | + int c = a+b; |
| 17 | + return c; //Return value |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +- In the above method, int is the return data type of the mySum function. |
| 22 | +- mySum takes two parameters: int a and int b. |
| 23 | +- The sum of two values integer values(a and b) is stored in another integer value named 'c'. |
| 24 | +- mySum returns c. |
| 25 | + |
| 26 | +### Calling a Method : |
| 27 | +- A method can be called by creating an object of the class in which the method exists followed by the method call: |
| 28 | +``` |
| 29 | +Calc obj = new Calc(); //Object Creation |
| 30 | +
|
| 31 | +obj.mySum(a , b); //Method call upon an object |
| 32 | +``` |
| 33 | +- The values from the method call (a and b) are copied to the a and b of the function mySum. Thus even if we modify the values a and b inside the method, the values in the main method will not change. |
| 34 | + |
| 35 | +### Void return type : |
| 36 | +- When we don’t want our method to return anything, we use void as the return type. |
| 37 | + |
| 38 | +### Static keyword : |
| 39 | +- The static keyword is used to associate a method of a given class with the class rather than the object. |
| 40 | +- You can call a static method without creating an instance of the class. |
| 41 | +- In Java, the main() method is static, so that JVM can call the main() method directly without allocating any extra memory for object creation. |
| 42 | +- All the objects share the static method in a class. |
| 43 | + |
| 44 | +### Process of method invocation in Java : |
| 45 | +- Consider the method Sum of the calculate class as given in the below code : |
| 46 | + |
| 47 | +``` |
| 48 | +class calculate{ |
| 49 | + int sum(int a,int b){ |
| 50 | + return a+b; |
| 51 | + } |
| 52 | +``` |
| 53 | + |
| 54 | +- The method is called like this: |
| 55 | + |
| 56 | +``` |
| 57 | +class calculate{ |
| 58 | + int sum(int a,int b){ |
| 59 | + return a+b; |
| 60 | + } |
| 61 | +
|
| 62 | +public static void main(String[] args) { |
| 63 | + |
| 64 | + calculate obj = new calculate(); |
| 65 | + int c = obj.sum(5,4); |
| 66 | + System.out.println(c); |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | +``` |
| 71 | +Output : 9 |
| 72 | +``` |
| 73 | +- Inside the main() method, we've created an object of the calculate class. |
| 74 | +- obj is the name of the calculate class. |
| 75 | +- Then, we've invoked the sum method and passed 5 and 4 as arguments. |
| 76 | + |
| 77 | +**Note:** In the case of Arrays, the reference is passed. The same is the case for object passing to methods. |
| 78 | + |
| 79 | +### Code as described in the video: |
| 80 | +``` |
| 81 | +package com.company; |
| 82 | +
|
| 83 | +public class cwh_31_methods { |
| 84 | +
|
| 85 | + static int logic(int x, int y){ |
| 86 | + int z; |
| 87 | + if(x>y){ |
| 88 | + z = x+y; |
| 89 | + } |
| 90 | + else { |
| 91 | + z = (x +y) * 5; |
| 92 | + } |
| 93 | + x = 566; |
| 94 | + return z; |
| 95 | + } |
| 96 | +
|
| 97 | +
|
| 98 | + public static void main(String[] args) { |
| 99 | + int a = 5; |
| 100 | + int b = 7; |
| 101 | + int c; |
| 102 | + // Method invocation using Object creation |
| 103 | + //cwh_31_methods obj = new cwh_31_methods(); |
| 104 | + //c = obj.logic(a, b); |
| 105 | + c = logic(a, b); |
| 106 | + System.out.println(a + " "+ b); |
| 107 | + int a1 = 2; |
| 108 | + int b1 = 1; |
| 109 | + int c1; |
| 110 | + c1 = logic(a1, b1); |
| 111 | + System.out.println(c); |
| 112 | + System.out.println(c1); |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +**Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-31/Ch7.pdf)** |
| 118 | + |
| 119 | +**Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-31/UltimateJavaCheatSheet.pdf)** |
0 commit comments