Our social:

Wednesday, August 17, 2016

Methods in Java – How Can their use and working-Best Education service

Methods in Java

[Tutorial 9] :Methods in Java – Their use and working

Methods in Java are used to perform a specific function. Remember the Tutorial 7 : Objects and Classes in Java where I wrote about how objects are implemented in software terms. In that same column, I also mentioned that the behaviour of the object is implemented through members functions called methods.
Now I hope you have established  a connection between Objects and Methods. The behaviour of the objects is implemented using methods.
In this module I will be covering the answers to the following questions :-
  • What are methods in Java ? 
  • Why we need methods ? 
  • How to define methods in Java programs ? 
  • What is the method signature ?
  • How to access  methods ?
Lets us start. But wait, before starting , let me make a simple point clear to you.
Methods in Java are also known as Functions, so make sure that you do not get confused between these two.

What are Methods in Java ?

Methods in Java are defined as
Methods are a set of statements in Java that are used to perform a specific task. Methods are also known as functions in Java.

Why do we need methods ?

There are three main reasons why we need methods :-
  • To cope with complexity : When we need to do huge coding to make big Java Programs , we have a huge set of codes. Dividing the large program is the best way to make the program look simpler which is done using methods or functions.
  • Hiding important details : Once a method is created , it can be used in the program and we need not concern ourselves with how to function is performed. So , Methods can be very useful in hiding the important details that you will not want to show in your program.
  • Using again and again : It can be possible that we need to use a method again and again and there should be something so that we need not write the same piece of code again and again. That something is known as “Methods in Java

How to define methods in a Java Program ?

In order to define a method in a Java Program , a generic syntax is followed which is given below
[access-specifier] + [modifier] + return-type  + method-name + (parameter list)
Well , now try to recall the first program that I taught you. Let me put is here only for your convenience 😀
public class mydetails
{
                public static void main()
                 { 
                          System.out.println(“Hello !! My name is Pranshu Kharkwal”);
System.out.println(“I am learning java”);
}  

}
In this program , the area which is in “BOLD” is the method. This is a simple program and so it contains only one method. In future, you will be learning more complex programs where you will need to use many methods and this knowledge will be very helpful.
Now coming to our main query , How to define methods in Java ?
See the first line of the method ” public static void main() ”
Now try to relate it to the syntax that I gave you ” [accress-specifier] + [modifier] + return-type  + method-name + (parameter list) ”
In case you are confused , let me compare them 😀
access specifier – public
modifier – static
return type – void
method-name – main (or anything you want)
parameter list – ()  [Anything can be added here. I have told about parameters in Tutorial 5]
Let me tell you about all these in detail now
  • Access Specifier – It can be either public or protected or private or default. They are used to determine the type of access to the function. More about this will be covered in the upcoming tutorials.
  • Modifier – It can be final , native , synchronized , transient , volatile or static.
  • return_type – It can be of any of the Java Primitive Data Types and if none of them, it is “void”
  • method-name – It can be anything according to your choice but make sure it is somewhat relevant to your program.
  • parameter list – All the arguments that you want to be listed as parameters.

What is method prototype and method signature ?

Method Prototype

A method prototype is the first line of the method that includes access specifier , modifier , return type , method name and parameter list (optional)
Examples of method prototype are :
  1. public static void main
  2. void example
  3. public void ejavatutorials
  4. void addition

Method Signature

A method signature generally refers to the number and the type of arguments passed to the method.  It basically refers to the parameters. 
A method signature comes under method prototype only.
Examples of Method Signature are
  1. public static void main(int a , int b)
  2. void example(float f)
  3. public void ejavatutorials(String Java)
  4. void addition(char ch)

How to access methods  ?

Accessing a method is also known as calling a method or invoking a method. A method can be called or invoked by providing the method name , followed by the parameters being sent enclosed in parenthesis.
For example :
int addition(int_______ , int_______)
Here is a program where the use of method in illustrated.
public class calculator
{
       int result = 0;
             public void addition(int a , int b)
            {
                result = a+b;
System.out.println(result);

            }
           public void subtraction(int c  , int d) 
            {
                result = c-d;
System.out.println(result);

            }            
           public static void main()
          {
             calculator c1 = new calculator();
             c1.addition(10 , 20);
             c1.subtraction(30 , 40);
         }
}
In this program , I created an object and then called the methods using the object.

How to create objects ?

It is very easy to create objects. You can create an object by using the following syntax :
class name + object name + ‘=’ + ‘ new ‘ + class name + ‘ (); ‘
Now compare this syntax with the way I created  the object in the above program and you will understand how to create objects. Use the object to call the methods.
There are few other topics that come under methods which I will cover later on. As of now , Let us stop here only.

0 comments:

Post a Comment

Hello Every One thanks For Visit My site You Can Suggest any Idea About site And Related Education Information Provide me Its Best Content For you am Include in this Site And feel free contact And email now I hope This site very help full you