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 Java Method?

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 that 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 the method's return type as "Void".
  • The method name should be different from the class name. If you give the same name to the method as the class name then it would become Constructor.
  • We can also pass some input to the Java methods which are known 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 {}.
  • The method is basically created for the 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 the method by making it public, protected, or default.
  • Static - You can also declare a method static by adding a keyword static in the method's definition. Method's without Static keywords are called non-static methods. We'll cover this topic in detail later in this post.
  • Return Type - Secondly, the 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 anything 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., the first word of the method name will be a small case, but the first letter of the second word will be upper case and the rest will be small. Like this., myMethod, longMethodName. Method names can't have special characters.
  • Parameters - Inputs provided to a method are known as parameters. They can be of any type like., string, char, int, boolean, 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 declare 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 the Main method in Java?

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

Syntax for the 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 the main method:

  • Public is the access specifier. It's made as a 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 the 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  Java Methods

Java Methods can categorize into the following:

  • Static Methods
  • Non-Static Methods

4.1. Static Methods

Java methods that are declared with the keyword 'static' are known as static methods. They are also called' Class Related' methods. Other few noticeable points about static methods are:
  • Static methods or variables can be called inside static methods without instantiating the object of the class.
  • Static methods from other classes can be called using the 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.

4.2. Non-Static Methods

Java Methods that are declared without static keywords are non-static methods. To call these methods we have to instantiate the 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.");
  }

}



5. Parameterized and Non-Parameterized Methods

5.1. 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 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.


5.2. 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");
  }



Click here to find the complete Java Tutorials Series

Java Class and Object  << Previous     ||     Next >>  Java Conditional Statements

Author
Vaneesh Behl
Passionately writing and working in Tech Space for more than a decade.

Comments

  1. You have a genuine capacity to compose a substance that is useful for us. You have shared an amazing blog about Java Programming Assignment Experts thanks for sharing this blog with us.

    ReplyDelete

Post a Comment

Popular posts from this blog

17 Best Demo Websites for Automation Testing Practice

Top 51 Most Important Selenium WebDriver Interview Questions

14 Best Selenium Practice Exercises for Automation Practice

Python Behave Tutorial: A Comprehensive Guide to Behavior-Driven Development (BDD)

Automation Practice: Automate Amazon like E-Commerce Website with Selenium

Mastering Selenium Practice: Automating Web Tables with Demo Examples

Mastering User Interactions with the Actions Class in Selenium WebDriver

Handle Multiple Browser Tabs and Alerts with Selenium WebDriver

Mastering Selenium WebDriver: 25+ Essential Commands for Effective Web Testing

Java Regular Expressions: A Comprehensive Guide with Examples and Best Practices