What are Java Methods?

In this post, we'll learn everything about Java Methods.

Table of Contents

  1. What is a Java Method?
  2. How to Create a Method?
  3. What is a Main method in Java?
  4. Types of Java Methods
    • Parameterized Methods
    • Non-Parameterized Methods
  5. How to Call a Method?
    • Static Methods
    • Non-Static Methods


1. What is a method in Java?

A method in java is a group of instructions that are performed together to do a specific task. Methods are generally known as Functions in other languages like C, C++ etc.  Let's say you want to print numbers 1 to 10 in your program multiple times. So, if you have created a method with code which can print 1 to 10 numbers then you can just call that method multiple times in your program and you do not have to write the code to print 1 to 10.

Java Methods have some features, let's take a look at them:

  • Java Method can be public, private, protected or default.
  • Java method can or cannot return a value. If you do not want your method to return anything, you have to declare method's return type as "Void".
  • Method name should be different from class name. If you give same name to the method as class name then it would become Constructor.
  • We can also pass some input to the java methods which are knows as parameters.
  • You can also throw any exception in the declaration statement of the method, which you expect from your method code.
  • Similar to class, a method starts and ends with parenthesis {}.
  • Method is basically created for re-usability of code.

2. How to Create a Java Method

There are certain rules/syntax to define a method in Java. Let's understand them one by one.

  • Access Modifier - First of all, you have to define the accessibility level of method by making it public, protected or default.
  • Static - You can also declare a method static by adding a keyword static in method's definition. Method's without Static keyword are called non-static methods. We'll cover this topic in detail later in this post.
  • Return Type - Secondly, method should have a return type like., String, int, boolean etc. It depends on the value, which this method will return. If your method doesn't return any thing then your method's return type will be declared as "void".
  • Method Name - Then comes the method name. In java, we use Camel Casing while declaring the names of methods. Like., first first word of the method name will be smallcase, but the first letter of second word will be upper case and rest will be small. Like this., myMethod, longMethodName. Method name can't have special characters.
  • Parameters - Inputs provided to method are known as parameters. They can be of any type like., string, char, int, boolena etc.
  • Method Body - It contains all the code that needs to be executed when that method will be called. It starts and ends with parenthesis {}.
Syntax to decalare a Java Method name myMethod inside class MyClass:

public class MyClass {

  public void myMethod() {
    // code to be executed
    System.out.print("Hello World");
  }

}

Let's explain the above sample code,

  • myMethod() method is declared inside MyClass.
  • myMethod() is declared as public. It means this method is accessible to all the other classes of your project.
  • myMethod()'s return type is void. It means it'll only execute the code inside it and will not return anything.
  • myMethod() will print "Hello World", that's the method body.

3. What is Main method in Java?

Main method is the execution point of any Java program. JVM starts the Java program execution from main method. It is a public method and also declared as static. It's return type is void means null.

Syntax for Main method in Java

public class MyClass {

  public static void main(String args[]) {
    // code to be executed
    System.out.print("Hello World");
  }

}

Let's go word by word for main method:

  • Public is access specifier. It's made as public method so that JVM can invoke it from outside the class.
  • Static means main is a class related method. JVM can invoke it without instantiating object of the class.
  • Void means it won't return anything.
  • Main is the name of the method.
  • String args[] is an array of arguments. 


4. Types of Methods in Java

Java Methods can categorized into following:

  • Parameterized and Non-Parameterized Methods
  • Static and Non-Static Methods

Parameterized Methods
Java Methods which have parameters provided are known as parameterized methods. Example code,

  public static void myMethod(String name, int age) {
    // code to be executed
    System.out.print(name);
    System.out.print(age)
  }

Let's explain the above sample code,

  • We already discussed about public, static, void and main.
  • Two parameters are passed to myMethod(), which are String name and int age.
  • myMethod() will print the value of name and int, that's the method body.


Non-Parameterized Methods
Java Methods which don't have parameters are known as parameterized methods. Example code,


  public void myMethod() {
    // code to be executed
    System.out.print("Hello World");
  }


5. How to Call a Method

Static Methods
Java methods which are declared with keyword 'static' are known as static methods. They are also called as 'Class Related' methods. Other few noticeable points about static methods are:
  • Static methods or variables can be called inside static methods without instantiating object of the class.
  • Static methods from other classes can be called using Class name and dot operator. Like., MyClass.myMethod()

public class MyClass {

  public static void main(String args[]) {
    // Call to static method
    myMethod();
  }

  public static void myMethod() {
    // code to be executed
    System.out.print("This is static method.");
  }

}

Output:
This is a static method.

Non-Static Methods
Java Methods which are declared without static keyword are non-static methods. To call these methods we have to instantiate object of the class and only then we can call it.

public class MyClass {

  public static void main(String args[]) {
    // Call to non-static method
    MyClass obj = new MyClass();
    obj.myMethod();
  }

  public void myMethod() {
    // code to be executed
    System.out.print("This is static method.");
  }

}


Happy Learning!

Follow Us

Quora Space | Facebook Page | Telegram Channel
Feel free to ask queries or share your thoughts in comments or email us.

Comments

Popular posts from this blog

10 Best Demo Websites for Selenium Automation Testing Practice

Automate an e-Commerce website with Selenium Webdriver

Automate Browser Actions and Menu links with Selenium Webdriver

Top 25 Selenium Webdriver Commands for Test Automation

How to Handle Dynamic Web Table in Selenium WebDriver

Automate Google Search with Selenium Webdriver