What is Method

What is Method

 
Method is a collection of statements which is referred by a name and can be invoked at any point of time.
 
Why Method is required
 
In general practice,  to perform specific operation, we create an individual methods. It makes the code clean and more readable.
 



package com.diaryreaders.corejava.method;
public class Method {
private static void execute(String s){
System.out.println(s);
}
public static void main(String[] args) {
String s = "execute";
execute(s);
}
}


 

In above code, execute is the method which has one “{” opening curly braces and one “}” closing braces. Whatever statements we want to perform in this method would be written in the opening and closing braces i.e. Method body.
 
What is Method Signature
 
Method signature is the method name + arguments types. In the above case, It would be execute(String s).
 
Can a method be written with blank method body?

Yes, Its valid.

 



private void Dummy(){
}


 

Can a method be written with no method body?

Yes, But only in abstract class with abstract keyword and interfaces.

 



package com.diaryreaders.corejava.method;
public abstract class AbstractClass {
abstract void print();
}
interface TestInterface{
void print();
}


No Comments Yet

Leave a Reply

Your email address will not be published.

Lorem ipsum dolor sit amet, consectetur a dipiscing elit. Vivamus leo ante,

FOLLOW US ON