METHODS DECLARATION IN JAVA

A method is a function. That is, it is a block of code that carries out an operation. Like fields,
methods need to be inside classes. A method can accept values from other parts of the program,
and they can send results back to other parts of the program.
1. // Below is a method within Book
2. public void printNotice() {
3. System.out.println("I'm a Book!");
4. }
A method can also have a type, as a field would. If the method is not going to be sending any
information to other parts of the program, the type is set as void. However, if a method will be
accepting and returning information, you need to specify which type of information. Let's see
how this works with parameters and returning information.
If a method is set up to receive information, these values are called parameters. A parameter is a
field that is sent to and from methods. The following code creates a method that will return an
integer value. Therefore, it needs to have a type of int when the method is defined.
1. public int setCount(int c, String isbn) {
2. int count = c + 15;
3. String myIsbn = isbn;
4. return c;
5. }
Posted on by