Methods in Java

In this class, We discuss Methods in Java.

The reader should have Intuition in Class and Object. Click Here.

A Class contains the following.

1) Data Members

2) Methods

3) Constructors

4) Nested Classes

5) Interfaces

In this class, we understand methods.

Method:

A method is a block of code that takes input, does some action, and gives an output.

Example:

class methodexample

{

int add(int a, int b)

{

int c;

c=a+b;

return c;

}

}

Int add(int a, int b) is the method signature.

“add” is the method name.

The add method takes two input values of type integer.

The int written before the method name is the return type.

The add method returns a value of type integer.

The code written between flower brackets is the body of the method.

Methods:

1) Non-Static or instance methods

2) static methods

We discuss static methods in our later classes.

Static int add(int a, int b) is the static class.

Use static keywords to make the method static.

Void display()

{

System.out.println(“hello”);

}

The above method is not taking any inputs and returns nothing.

Void is used to mention no type.

How to use these methods is discussed in the next class.