Loading

Methods

  • The method means a block of code that executes when we call.
  • It describes the behaviour or logic of an object.

Syntax:

access_modifier return_type name_Of_Method(parameters){
       //body
}     

 Example:

public void helloWorld( int a){
	//code
} 

Key Points: -

  • It is a Java convention that the method name should be in camelCase format (i.e. method_Name ).
  •  It enhances the readability and reusability of a program.
  • The most common method used in Java is the main() method where the compiler starts compiling.

A parameter in Method: -    

  • It is the data passed through parameters that are listed after the method name and are enclosed in parentheses. e.g. printString(String msg).

Example :

package quipohouse;
public class Main
{
  public static void main(String[] args)
  {
    String name = "Quipo House";
      message(name);
  }
  static void message(String name)    //here name is a parameter
  {
    System.out.println(name);
  }
}      

Output:

Quipo House

Arguments :

  •  When a parameter is passed to the method using the command line or main method is called an argument.

Example:

package quipohouse;
public class Main
{
  public static void main(String[] args)
  {
    //arguments that passed in to the parameters
    message("HELLO QUIPO HOUSE");
  }
    //parameters
  static void message(String message)
  {
    System.out.println(message);
  }
}                                                                                                      

Output:

HELLO QUIPO HOUSE

 Types Of Methods

1. Predefine Method:-

  • It is often referred to as the built-in approach.
  • This method type is already defined in the Java library or we can say that methods provided by Java itself are predefined methods.

 

 

Example:

package quipohouse;
public class Main
{ 
  // main() method is predefined method
  public static void main(String[] args)
  {
    String name = "QUIPO HOUSE";
      message (name);
  }
  static void message(String name)
  {
    System.out.println(name);
  }
}       

Output:

QUIPO HOUSE

Other examples are- Math.max(), Math.min() etc.


2. User-Defined Method:-

  • It is defined by the programmer or coder.

Example:

package quipohouse;
public class Main
{
  public static void main(String[] args)
  {
    String name = "Quipo House";
      message(name);
  }
  
  //Here message is user Define method
  static void message(String name) 
  {
    System.out.println(name);
  }
}   

Output:

Quipo House

3. Static Method:-

  • When a method is defined with the keyword "Static," it signifies that it belongs to the class and not to an instance of the class.
  •  We can call a static and not static method without the help of an object inside the static method.
  • The static method can also be called by class name or directly.

Example:

package quipohouse;
//here main method is defined with the keyword static
public class Main
{
  public static void main(String[] args)
  {
    String name = "Quipo House";    
    //arguments that passed in to the parameters
     message(name);         //calling directly
     Main.message(name);    //calling by className
     Main m=new Main();
     m.message(name);       //calling by object
  }
 
  //Here message (user Define method) also a static method
  static void message(String name) 
  {
    System.out.println(name);
  }
} 

Output:

QUIPO HOUSE
QUIPO HOUSE
QUIPO HOUSE

4. Instance Method: -

  • It is also called a non-static method.
  • In this method, we need to create an object of the class and by that object, we call the methods.

Example:

//File : Main.java

package quipohouse;
public class Main
{
  public static void main(String[] args)
  {
    //here we are creating an object for calling the method
    InstanceMethod obj = new InstanceMethod();

    //arguments that passed in to the parameters
    obj.Message("HELLO QUIPO HOUSE");
  }
}
//File : InstanceMethod.java

package quipohouse;
public class InstanceMethod
{
  public void Message(String message)	                            
  {
    System.out.println(message);
  }
}

Output:

 HELLO QUIPO HOUSE

5. Abstract Method: -

  • A method with no body is called the abstract method. It is marked as abstract.

Syntax:

abstarct void method_name();

Example:

//File : Main.java

package quipohouse;
abstract class Message
{
  //abstract method which it has no body.
  abstract void greets();

  // concrete methods are  allowed in abstract classes
  void name()
  {
    System.out.print("QUIPO HOUSE");
  }
}

// concrete class Greet
class Greet extends Message
{

  // class Greet must override greets() method
  void greets()     //method overriding
  {
    System.out.print("HELLO ");
  }
}

public class Main
{
  public static void main(String args[])
  {
    Greet obj = new Greet();
      obj.greets();
      obj.name();
  }
}

Output:

HELLO QUIPO HOUSE