Java – Methods


What is a method ?

Method is defined as set of systematic procedure followed to attain a particular goal.

What is a method in programming language ?

Methods in programming language is considered as block of standard codes which can be reused by calling multiple times inside the program which provides the desired output, without re-typing the code again and again.

What is method in java ?

Method in java is defined as set of systematic procedural codes executed in java program for attaining some result.

This is also termed as the block of code which is executed at any point of the program body for attaining some specific results.

Every method will have some name with some block of codes for doing repetative similar tasks again and again inside the program.

What are the types of java functions ?

There are two types of function

1. Java – built-in Function

2. Java – User defined Function

1. Java Built-in Function :

This is the function which is already defined in java class libraries.

We cannot modify these function, as its protocol is already defined.


Example :

public class Sys{
public static void main(String args[])
{
     System.out.println("Hello World");
}
}

Explanation:

public – java keyword which is an access specifier for the main method.

static – java keyword which is an access specifier, since its static we can call this method directly without creating object of the class.

void – java keyword which specifies the return type of the method, since its void it returns nothing.

main – java keyword which specifies the main method of the java program.

String(args[]) – java keyword which specifies the String array of values can be passed to the main method as arguments.

System – Its a Pre-defined class in Java Language package ( java.lang ).

out – Its an instance of the Java Input/Output PrintStream in the package (java.io.PrintStream ).

println – Its the method of PrintStream class.

So these methods cannot be modified by the programmer, only we can call this methods at any point of the program to get the desired output.

2. Java User-Defined Methods :

These methods are created by the User, which can also be called inside the java program anywhere to get the desired output.

Example :

public class Methods{
        public static void main(String args[])
                {
                int a = 10;
                System.out.println("value of a inside main method = " +a);
                MethodOne();
                }
        public static void MethodOne(){
                int b = 20;
                System.out.println("value of b = " +b);
                }
}

Output :

value of a inside main method = 10

value of b = 20

Here user defined method is MethodOne() which is called inside the main method and the code executed successfully.

Declaring a Method :

1. Method should have a specific access-specifier.

2. Method should have a return type.

3. Method should have a specific name.

4. Method may or may not have a parameter list as arguments.

Syntax :

{Access-Specifier} {Return-type} {Method-Name} {Param-List}

{Access-Specifier} {Access-Specifier} {Return-type} {Method-Name} {Param-List}

public static int methodName (Parameter list)
{
// methodBody
....
....
....
}

Calling a Method :

1. Calling a method inside the class 

Methods called inside the class.

Example :

public class JavaMethods{
public static void main(String args[])
{
    System.out.println("calling from main method");
    MethodOne();
}
public static void MethodOne()
{
  int a = 10;
  int b = 20;
  String country = "India";
  System.out.println("Printing from MethodOne()");
  System.out.println("The value of a = " + a);
  System.out.println("The value of b = " + b);
  System.out.println("The value of country = " + country);
}
}

Output :

calling from main method

Printing from MethodOne()

The value of a = 10

The value of b = 20

The value of country = India

Since return-type is mentioned as void, MethodOne() doesn’t return any value.

2. Methods returning values 

Methods with return type returning values ( void return type is exemption since it returns nothing )

Example :

public class JavaMethods{
public static void main(String args[])
{
  System.out.println("calling from main method");
  System.out.println("Printing the return value from MethodOne() = "+MethodOne());
  System.out.println("Printing the return value from MethodTwo() = "+MethodTwo());
  System.out.println("Printing the return value from MethodThree() = "+MethodThree());
}
public static int MethodOne()
{
  int a = 10;
  return a;
}
public static int MethodTwo()
{
  int b = 20, c = 30;
  return b+c;
}
public static String MethodThree()
{
  String country = "India";
  return country;
}
}

Output :

calling from main method

Printing the return value from MethodOne() = 10

Printing the return value from MethodTwo() = 50

Printing the return value from MethodThree() = India

3. Methods with parameter values.

Methods invoked with parameters, and these parameters are passed as argument for the method.

Example :

public class JavaMethods{
public static void main(String args[])
{
  int result = MethodOne(100,400);
  System.out.println("The result value from MethodOne() function = = " + result);
  String FullName = MethodTwo("Mohandas","Karamchand Gandhi");
  System.out.println("The result value from MethodTwo() function = = " + FullName);
  String Name = MethodThree("AbdulKalam",'A','P','J');
  System.out.println("The result value from MethodTwo() function = = " + MethodThree("AbdulKalam",'A','P','J'));
}
public static int MethodOne(int a,int b)
{
  return a+b;
}
public static String MethodTwo(String firstName, String LastName)
{
  return firstName+" "+LastName;
}
public static String MethodThree(String Name, char char1, char char2, char char3)
{
  return char1+"."+char2+"."+char3+"."+Name;
}
}

Output :

The result value from MethodOne() function = = 500

The result value from MethodTwo() function = = Mohandas Karamchand Gandhi

The result value from MethodTwo() function = = A.P.J.AbdulKalam

4 . Method Overloading 

When a class have two methods with same Name and its return value is different , The methodName will get Overloaded when called inside.

Example :


public class JavaMethods{
public static void main(String args[])
{
  MethodOne(100,400);
  MethodOne("Mohandas","Karamchand Gandhi");
  MethodOne("AbdulKalam",'A','P','J');
  System.out.println("The result value from MethodOne() function = = " + MethodOne(100,400));
  System.out.println("The result value from MethodOne() function = = " + MethodOne("Mohandas","Karamchand Gandhi"));
  System.out.println("The result value from MethodOne() function = = " + MethodOne("AbdulKalam",'A','P','J'));
}
public static int MethodOne(int a,int b)
{
  return a+b;
}
public static String MethodOne(String firstName, String LastName)
{
  return firstName+" "+LastName;
}
public static String MethodOne(String Name, char char1, char char2, char char3)
{
  return char1+"."+char2+"."+char3+"."+Name;
}
}

Output :

The result value from MethodOne() function = = 500

The result value from MethodOne() function = = Mohandas Karamchand Gandhi

The result value from MethodOne() function = = A.P.J.AbdulKalam

5 . Method Calls with Object

Methods can also be called from object. 

Example :

class JavaMethods{
public static void main(String args[])
{       
        JavaOutput obj = new JavaOutput();
        int c = obj.MethodOne(100,500);
        System.out.println("The value of c = " + c);
}
}
class JavaOutput
{
public int MethodOne(int a,int b)
{
        return a+b;
}
}

Output :

The value of c = 600

6. Method calling from another methods

Methods calling another methods there by executing the statements.

Example :

public class MethodsCalls{
        public static void main(String args[])
                {
                        System.out.println("Printing inside main method");
                        MethodOne(5,10);  // calling MethodOne inside main method
                }
        public static int MethodOne(int a,int b)
        {
          int c = a+b;
          System.out.println("Printing inside MathodOne and the value of c =" +c);
          MethodTwo('A','B','C','D'); // calling MethodTwo inside MethodOne
          return c;
        }
        public static char MethodTwo(char a, char b, char c, char d)
        {
          System.out.println("Printing inside MethodTwo and return char values = "+a+","+b+","+c+","+d);
          MethodThree("Abdul","Kalam"); // calling MethodThree inside MethodTwo
          return a;
        }
        public static String MethodThree(String firstName, String lastName)
        {
          System.out.println("Printing inside MethodThree and the return value = " + firstName + lastName);
          return firstName+lastName;
        }
}

Output :

Printing inside main method

Printing inside MethodOne and the value of c =15


Printing inside MethodTwo and return char values = A,B,C,D

Printing inside MethodThree and the return value = AbdulKalam

7. Static method in java

Java static methods can be called without creating an object of the class.

Static keyword can be used with class, variable, method and block.

Why are we using static keyword while defining main method?

Its because the program executes the main method without creating the object of the class.

class Test {
  public static void main(String[] args) {
    showOutput();
  }
  static void showOutput() {
    System.out.println("Never Ever Give UP !!!!!");
  }
}

Output :

Never Ever Give UP !!!!!