Wednesday, 14 February 2018

Methods in Java


Methods

A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method’s name. Think of a method as a subprogram that acts on data and often returns a value. Each method has its own name.
Types of Java Methods :- There are 2 types of methods:
1. Standard Library Methods
2. User-defined Methods

1. Standard Library Methods

These are built in methods in java that are readily available for use. These standard libraries come along with the java class library(JCL) in a java archive(.jar) file with JVM and JRE.
  • Print() is a method of java.io.PrintStream. The print(“.....”) prints the string inside the quotation marks. Eg.

Program :-

Public class Numbers{
Public static void main(String[] aa){
System.out.print(“Hello java”);
}
}
When you run the program, the output will be:
Hello java


2. User-defined Methods

You can also define methods inside a class as per our wish, In this methods are defined as per the user’s choice.
  • Creation of User-defined Method :-

Before call a method(or use a method), we need to define it. Eg.

Public static void myMethod(){ //myMethod() is defined
System.out.println(“My function called”);
}


You can see 3 keywords public, static and void in the creation of methods, lets discuss about it :-

  • The public keyword makes myMethod() method public. Public members can be accessed from outside of the class.
  • The static keyword denotes that the method can be accessed without creating the object of the class.
  • The void keyword signifies that the method doesn’t return any value.

  • How to call a Java Method ?

Now you need to use a defined method in your class. For that you have to call the method.
myMethod(); //this statement calls the myMethod()


Program of Java Method

Class Main{
Public static void main(String aa[]){
System.out.println(“creation of method”);
myMethod(); // method call
System.out.println(“method was executed successfully!”);
}
Public static void myMethod(){ // method definition
System.out.println(“it is my method definition”);
}
}

When we run it, the output will be :-

Creation of method
Method was executed successfully!
It is my method definition

Note that, we call the method without creating object of the class. It was because myMethod() is static. Consider another example, in this our method is non-static and is inside another class.

Program :-

Class Main{
Public static void main(String aa[]){
Output obj=new Output();
System.out.println(“hello world”);
// calling myMethod() of Output class
Obj.myMethod();
}}
Class Output{
// public: this method can be called from outside the class
Public void myMethod(){
System.out.println(“my method called”);
}}


Types of Methods:-



  1. Parametrized methods:- These methods contain a parameter list/argument list which recives a value from the calling method. For eg.
Public int sum(int a, int b){
//body
}
Program:-
Import java.util.scanner;
Class callbyvalue
Public static void main(String aa[]){
Scanner in=new Scanner(System.in);
Int x,y;
System.out.println(“Enter 2 numbers:”);
X=in.nextInt();
Y=in.nextInt();
Callbyvalue ob=new callbyvalue();
Ob.sum(x,y);
System.out.println(“the sum is:”+ob.sum(x,y));
}
Public int sum(int a,int b){
Int s=a+b;
Return s;
}


  1. Non-Parametrized methods:- These methods do not have any parameter-list. The programmer can simply call the function without sending any values to the function.
Program:-
Class ABC
{
public static void main(String aa[]){
add();
display();
}
Public static void add(){
Int a=1,b=2,c;
c=a+b;
System.out.println(c);
}
public static void display(){
{System.out.println(“Hello Java”);
}}


Here’s an eg of parametrized and non-parametrized methods:-