Methods in java

It is very excited tutorial for new programmers because after using number of built-in methods like print, println, random, etc. First summarize what we know about methods till now:

  • Methods are collection of statements which perform some task.
  • Method may have any argument (additional information).
  • Method may return some value.

And one more thing we know about java that we can call static type objects directly using name of the class. Now we are making our own methods.

Simple method

As you are already familiar with main method. The syntax of the method is:

  1. access_specifies type return_type method_name(argument_type argument_name,.....other_arguments....)
  2. {
  3. //code
  4. }

Compare this syntax with main method.

access_specifier => public

type => static

return_type => void ( void means this method is not returning anything, discussed later in this tutorial)

method_name => main

argument_type => String ( in fact array of strings)

argument_name => args ( you can change this name to anything)

Now we are writing a simple methods which will print their_type + "hello world".

  1. package flowkl;
  2. public class Tutorials
  3. {
  4. // private method for printing hello world
  5. private static void static_test()
  6. {
  7. System.out.println("static hello world");
  8. }
  9. private void dynamic_test()
  10. {
  11. System.out.println("dynamic hello world");
  12. }
  13. public static void main(String args[])
  14. {
  15. static_test();
  16. Tutorials object = new Tutorials();
  17. object.dynamic_test();
  18. }
  19. }

Note that we can call static methods of same class directly in main method but we need to make object for dynamic methods.

Methods have these properties:

  • Any static method can be called from any other method of the class (including main method).
  • Dynamic methods can be called directly only by dynamic methods. Static methods must create an object for calling dynamic methods.

Method with parameters

Just like main method, we can create methods which take some extra information called parameters. We use this information as a defined variable. For example in case of main method, we have a parameter of type array of String whose standard name is args (you can change the name). Look at example below. It is printing the values which we have pass it as arguments.

  1. package flowkl;
  2. public class Tutorials
  3. {
  4. private void method1(String name)
  5. {
  6. System.out.printf("name is %s \n",name);
  7. }
  8. private void method2(String name, int age)
  9. {
  10. System.out.printf("name is %s and age is %d \n",name,age);
  11. }
  12. public static void main(String args[])
  13. {
  14. Tutorials object = new Tutorials();
  15. object.method1("harish");
  16. object.method2("harish", 20);
  17. }
  18. }

NOTE: Arguments must be of data type that of parameters.

Method with variable parameters:

We can define a method which can accept variable arguments.

  1. package flowkl;
  2. public class Tutorials
  3. {
  4. private void sum(int... numbers){
  5. if(numbers.length == 0){
  6. System.out.println("No argument passed");
  7. return;
  8. }
  9. int sum = numbers[0];
  10. for(int i =1; i < numbers.length; i++)
  11. {
  12. sum+=numbers[i];
  13. }
  14. System.out.println("Total sum is "+ sum);
  15. }
  16. public static void main(String args[])
  17. {
  18. Tutorials object = new Tutorials();
  19. object.sum(1,2,3,4,5);
  20. object.sum(6,7);
  21. }
  22. }

It is program of summation of numbers. Look we are passing variable arguments to sum method. In line 8 return statement will skip the other code of the method after this line. We use this statement because there is no number to add.

Return type method

Till now we have used void type methods which does not return any thing. These methods just do task and print to the console. But now we want to use the output for further operations. So we need return type methods. Syntax of these methods is same except these methods have a return statement (with some value of same return type) in the last line. For these methods we have to write some data type or object (like class) type instead of void. This example is showing return methods with integer, double, string and array of integer.

  1. package flowkl;
  2. public class Tutorials
  3. {
  4. private int return_int()
  5. {
  6. int a = 8;
  7. return a;
  8. }
  9. private double return_double()
  10. {
  11. return 234243.343;
  12. }
  13. private String return_string()
  14. {
  15. return "harish";
  16. }
  17. private int[] return_array()
  18. {
  19. int[] array = {2,3,4};
  20. return array;
  21. }
  22. public static void main(String args[])
  23. {
  24. Tutorials object = new Tutorials();
  25. int i = object.return_int();
  26. double j = object.return_double();
  27. String s = object.return_string();
  28. int[] arr = object.return_array();
  29. System.out.printf("we have value of i = %d,"
  30. + "j = %.3f, s = %s",i,j,s);
  31. }
  32. }


ReactJS with Redux Online Training by Edureka

About Harish Kumar

Harish, a fullstack developer at www.lyflink.com with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and non-technical topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Complete Python Bootcamp: Go from zero to hero in Python 3

Top Posts

Help Dad unlock new skills this summer in just $10 on Udemy

Development Category (English)300x250

The Complete Web Developer Course - Build 25 Websites

Meet on Twitter

Subscribe

Subscribe now and get weekly updates directly in your inbox!

Best Video Courses