Java Methods | Types | Calling & Parameters Methods example

A Java Methods or Function is a group of the statement, which performs a particular operation. You can also call it’s a piece of code with its own functionally, and make your application code more neat and clean.

Don’t confuse with methods and Functions, both are the same. The different-different programming language used a different name for it.

Java Methods Types Calling & Parameters Methods example

This Tutorial you will about method declaration in java and important terms for them.

Java Methods Advantages 

Here are advantages of Java function.

  • Code reusability
  • Clean and effective coding, help to easy understand
  • Provide an easy testing environment

Syntax

Here is the simple syntax for it.

modifier returnType methodName (Parameter List) {
   // method body
}
Java Methods overview Parameters Methods example syntax

The syntax above syntax and image description is here, must read and understand.

  • modifier − Access type of method (public, protected,  private, and default), it’s optional.
  • returnType − Method return value data type(int, strings, Boolean etc).
  • methodName − A method name with parameter list or without parameters.
  • Parameter List − A pass the data, These are optional, the method may contain zero parameters.
  • method body − Statment and a logic area, to do some operations.

Java Methods Example 

Here is an example of Java function and How to call methods in Java. In this example, the function performs an adding 2 numbers and returning an integer value. Which methods have parameters, that functions have called parameter methods in java.

To access class methods we are creating an object (how to create java object).

class Hello {
    public static void main(String args[]) {

        // Creating class object
        Hello hello = new Hello();
        // pass 2 number to addNumber method
        System.out.println(hello.addNumber(2, 4));
    }

    protected int addNumber(int a, int b) {
        int c;
        c = a + b;
        return c;
    }
}

Output: 6

java functions 3600

Java Methods Types

  • Static methods – A method used static keyword, static methods can call without creating a class object.
  • Instance methods – This methods required an Instance variable of the class. <<class-object>>.<<method-name>>;
    • Accessor methods – Method to read the instance variable. These methods are also widely known as setter methods.
    • Mutator method – This method not only read instance variable but also modify the value of variables.
  • Factory methods –  A factory method is a method that returns an object to the class to which it belongs. All factory methods are static methods.

Other types mostly say like pre-defined java methods and user-defined methods in java.

QA: What is java helper methods

Java Helper methods assist other methods to do their job. You can see in a complex task, the Helper class execute some of the smaller tasks.

QA: How to call a void method in Java?

The void keyword allows us to create methods which do not return a value. It’s an easy check the below code.

class Hello {
    public static void main(String args[]) {

        Hello hello = new Hello();
        hello.message();
    }

    public void message() {
        System.out.println("Hello void");
    }
}

Output: Hello void

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java Function Examples are in Java 11, so it may change its different from Java 9 or 10 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.