How to use static method in java programming


Now moving to our next lesson ("static-method")What do you think is the "Static method"? well in this lesson you literally learn how to use the static-method and how you will implement it within your program, Any-way here is the definition of static method in java. A static method is a collection of a statement which are grouped together to perform an operation check out the program below to see how it works. Besides the usual (main method) it declares another method.

public class Main{
public static void main(String[] args){
printStr();
printStr();
printApple();
print("Hello");
printSum(20,40); //Sum method added
printSum(120,30); //Sum method added
int c = sum(20,40);//for static int sum method
int c = sum(140,200);
int d = multiply(30,40);// for multiply method
}
static void printStr(){
System.out.println("I'm a string"); //first static method
}
static void printApple(){ //second static method
System.out.println("Apple");
}
static void print() //third static method
{
System.out.println(String str);// string method
}
static void printSum(int a, int b) //sum method
{
System.out.println(a+b);
}
static int sum(int a, int b) //sum method with return type
{
return a+b;
}

static int multiply(int a, int b) //Product method with return type
{
return a*b;

}
}

in this case, the method printStr() is called from the main method Guess what the program does? The main method calls method printStr() which then prints "I'm a string" we call again the method printStr() to print the output string two times, now what happens is it print the output statement two times with the same result. We've added another method  printApple() which print "apple" now we call printApple() in the main method after the two call printStr(). Great! you've seen how methods can group of statements. Methods are even more powerful when they defined parameters check out the print method above it has a String parameter "str" The call of the method passes a String value of the parameter print("Hello"). Inside the method, then the parameter value can be accessed just like a variable. Guess what the program prints? The string value "Hello" which is passed as a parameter is printed. Now we call again the print() method to print on the next line "world". A method can also take more than one parameter. Check out the print sum method above we've added, it takes two integers as parameter print sum. The method also passes two values, one for each parameter printSum(20,40); the program prints the sum of the passed parameters 20 and 40. we call again printSum() to print the sum of 120 and 30 as well. Now you should see '150' result in a console window. Methods can not only execute statements they can also return a value. Check-out the sum method we've added for your exercises, it takes two integers as a parameter and returns the sum.
The "sum" method declares "int" instead of "void" as the return type accordingly. the call to "sum" returns the value passed to the return statement inside the method. int c = sum(20,40); The program prints the returned value of the sum method call. we call the sum again from the main method to calculate the sum of 140 and 200 and print the result, you should see "340" as a result, moving on to the next exercise write a Multiply method which will takes two integers as a parameter and returns the product of both, see the above codes for your answer.

Post a Comment

0 Comments