Method
Let us take a real world example to understand methods in Java. Let's say I want to get some loan from a bank. I approach to XYZ bank and ask for Rs. 2,00,000.00 loan. The bank person says he can give loan up to my eligible amount which depends on my salary and existing loan if any. So I give him the required data i.e. my current salary and existing loan amount. He calculates and tells me the maximum amount which he can give loan to me. I am not involved in the eligible amount calculation. I gave my data and the bank person calculates and gives me the result amount. This is the basic concept of method in java.
We specify a set of tasks inside a method. Whenever somebody calls the method, the method executes those tasks and gives a result. Please note that it is not always necessary to give a result from a method. Let's define a method for the example described above.
Here we specified the set of tasks that calculateEligibleLoanAmount method should execute. The tasks are: calculate the eligible amount as 15 times of the applicant's salary. If the applicant has any existing loan (i.e. existigLoanAmnt > 0) then deduct 10% of the existing loan amount from the eligible amount. Finally return the eligible amount.
So, we can say that a method must have a name (calculateEligibleLoanAmount), it may take some inputs (here the salary and the existing loan amount) and it may return some value (here the eligible loan amount).
The inputs to a method are called as Parameters or Arguments. The set of task that a method executes is called the method body. In this example the method is declared with public access modifier. When a method is declared with public access modifier, it can be called from other classes.
How to call a method
If a method is static, then it can be called by the class name. If the above method had been declared as static, then it would be called as:
If a method is non-static, then it is called by the object of the class. Below example shows calling a non-static method. Here 'l' is an object of class 'Loan' and the method is called using the object 'l'.
The salary and existing loan amount are passed within a pair of parenthesis. In this example 10000 is the salary and 3000 is the existing loan amount.
If a method doesn't return any value then the return type would be void.
If a public and static method named 'main' has void return type and takes a String array as argument, then it is a special method in Java. When we execute/run a program, the execution starts from main method.
Please note that the below declarations are also same as the main method we talked about.
Here the argument variable name is different (x) than the usual name args. In the second approach the array representation style is different (String[] x) but the meaning is same.
Below methods are not the main method we talked about. These can be treated like normal methods.
Here either the argument is not string array or the name is not 'main'.
I hope now you understand methods in Java. If you have any question, then write in comment. I will answer them.
Thanks,
S. R. Giri





Comments
Post a Comment