Advertisement
LegendSujay2019

Method in Java

Aug 25th, 2019
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.20 KB | None | 0 0
  1. Methods in Java
  2. A method is a collection of statements that perform some specific task and return the result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping the code. In Java, every method must be part of some class which is different from languages like C, C++, and Python.
  3. Methods are time savers and help us to reuse the code without retyping the code.
  4.  
  5. Method Declaration
  6.  
  7. In general, method declarations has six components :
  8.  
  9. Modifier-: Defines access type of the method i.e. from where it can be accessed in your application. In Java, there 4 type of the access specifiers.
  10. public: accessible in all class in your application.
  11. protected: accessible within the class in which it is defined and in its subclass(es)
  12. private: accessible only within the class in which it is defined.
  13. default (declared/defined without using any modifier) : accessible within same class and package within which its class is defined.
  14. The return type : The data type of the value returned by the method or void if does not return a value.
  15. Method Name : the rules for field names apply to method names as well, but the convention is a little different.
  16. Parameter list : Comma separated list of the input parameters are defined, preceded with their data type, within the enclosed parenthesis. If there are no parameters, you must use empty parentheses ().
  17. Exception list : The exceptions you expect by the method can throw, you can specify these exception(s).
  18. Method body : it is enclosed between braces. The code you need to be executed to perform your intended operations.
  19. methods in java
  20.  
  21. Method signature: It consists of the method name and a parameter list (number of parameters, type of the parameters and order of the parameters). The return type and exceptions are not considered as part of it.
  22. Method Signature of above function:
  23.  
  24.  max(int x, int y)
  25. How to name a Method?: A method name is typically a single word that should be a verb in lowercase or multi-word, that begins with a verb in lowercase followed by adjective, noun….. After the first word, first letter of each word should be capitalized. For example, findSum,
  26. computeMax, setX and getX
  27.  
  28. Generally, A method has a unique name within the class in which it is defined but sometime a method might have the same name as other method names within the same class as method overloading is allowed in Java.
  29.  
  30. Calling a method
  31.  
  32. The method needs to be called for using its functionality. There can be three situations when a method is called:
  33. A method returns to the code that invoked it when:
  34.  
  35. It completes all the statements in the method
  36. It reaches a return statement
  37. Throws an exception
  38. filter_none
  39. edit
  40. play_arrow
  41.  
  42. brightness_4
  43. // Program to illustrate methodsin java
  44. import java.io.*;
  45.  
  46. class Addition {
  47.      
  48.     int sum = 0;
  49.      
  50.     public int addTwoInt(int a, int b){
  51.          
  52.         // adding two integer value.
  53.         sum = a + b;
  54.          
  55.         //returning summation of two values.
  56.         return sum;  
  57.     }
  58.      
  59. }
  60.  
  61. class GFG {
  62.     public static void main (String[] args) {
  63.      
  64.         // creating an instance of Addition class  
  65.         Addition add = new Addition();
  66.          
  67.         // calling addTwoInt() method to add two integer using instance created
  68.         // in above step.
  69.         int s = add.addTwoInt(1,2);
  70.         System.out.println("Sum of two integer values :"+ s);
  71.          
  72.     }
  73. }
  74. Output :
  75.  
  76. Sum of two integer values :3
  77. See the below example to understand method call in detail :
  78.  
  79. filter_none
  80. edit
  81. play_arrow
  82.  
  83. brightness_4
  84. // Java program to illustrate different ways of calling a method
  85. import java.io.*;
  86.  
  87. class Test  
  88. {
  89.     public static int i = 0;
  90.     // constructor of class which counts
  91.     //the number of the objects of the class.
  92.     Test()
  93.     {
  94.         i++;
  95.          
  96.     }
  97.     // static method is used to access static members of the class  
  98.     // and for getting total no of objects  
  99.     // of the same class created so far
  100.     public static int get()  
  101.     {
  102.         // statements to be executed....
  103.         return i;
  104.     }
  105.  
  106.     // Instance method calling object directly  
  107.     // that is created inside another class 'GFG'.
  108.     // Can also be called by object directly created in the same class  
  109.     // and from another method defined in the same class  
  110.     // and return integer value as return type is int.
  111.     public int m1()
  112.     {
  113.         System.out.println("Inside the method m1 by object of GFG class");
  114.          
  115.         // calling m2() method within the same class.
  116.         this.m2();
  117.          
  118.         // statements to be executed if any
  119.         return 1;
  120.     }
  121.  
  122.     // It doesn't return anything as  
  123.     // return type is 'void'.
  124.     public void m2()  
  125.     {
  126.  
  127.         System.out.println("In method m2 came from method m1");
  128.     }
  129. }
  130.  
  131. class GFG  
  132. {
  133.     public static void main(String[] args)  
  134.     {
  135.         // Creating an instance of the class
  136.         Test obj = new Test();
  137.          
  138.         // Calling the m1() method by the object created in above step.
  139.         int i = obj.m1();
  140.         System.out.println("Control returned after method m1 :" + i);
  141.          
  142.         // Call m2() method
  143.         // obj.m2();
  144.         int no_of_objects = Test.get();
  145.          
  146.         System.out.print("No of instances created till now : ");
  147.         System.out.println(no_of_objects);
  148.          
  149.     }
  150. }
  151. Output :
  152.  
  153. Inside the method m1 by object of GFG class
  154. In method m2 came from method m1
  155. Control returned after method m1 :1
  156. No of instances created till now : 1
  157. Control flow of above program:
  158.  
  159. methods in java
  160.  
  161. Memory allocation for methods calls
  162.  
  163. Methods calls are implemented through stack. Whenever a method is called a stack frame is created within the stack area and after that the arguments passed to and the local variables and value to be returned by this called method are stored in this stack frame and when execution of the called method is finished, the allocated stack frame would be deleted. There is a stack pointer register that tracks the top of the stack which is adjusted accordingly.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement