Skip to content
APPCITORThe Tech Blog
Esc

Part 15 – Java Methods (Rules for methods)

July 31, 2020 2 min read

Part 15 – Java Methods (Rules for methods)

In the last 2 articles of Java methods you learned about the types of java methods and method-call stack. Before you going to the today’s topic (rules for methods) you can go to the link given below to take look at on the last 2 articles.

Part 13 – Java Methods (Introduction)

Today we are going to learn about the rules for methods under the topic java methods.

Let’s see what are those rules with the examples.

Rule no. 1

Return type of a method should be write with the method name.

Example:

Rule no. 2

Java program is running from the Main method. Therefore a method you create inside the class should be call in the main method.

Example:

Rule no. 3

There is no rule for method declaration order inside the class. It can be before or after main method.

Example:

Rule no. 4

We cannot define a method inside a method.

Example: Should not define a method inside main method.

Rule no. 5

If you create the method inside another class, you should call the method by the class name inside the main method.

Example:

Rule no. 6

If you put 2 variables for the parameter of the method, you should give their data types separately.

Example: Below given code is illegal.

Rule no.7

If you already defined a variable for the parameter, you don’t want to define it again inside the method body.

Example:

Rule no. 8

When you call a method inside the main method, the arguments should match with the parameters of the method.

Example:

Rule no.9

When you call a method inside the main method, the arguments should compatible with the parameters of the method.

Example: lines 4, 5, 6, 7 are illegal

Rule no. 10

If the argument you pass to the method is integer, Method will take it as 32 bits.

Example:

Rule no. 11

We can’t return values from a void type method.

Example: return z; statement is illegal.

Rule no. 12

If the method is a return type method, it is compulsory to return a value inside the method body.

Example: Code will give an error.

Rule no. 13

If the method is a return type method, the return value should be the last statement inside the method body.

Example: Code will give an error.

Rule no. 14

If the method is not defined correctly, compiler will give an error.

Example: lines 4 and 6 will give errors.

Rule no. 15

Compiler will not return values for any argument given for a method inside the main method.

Example:

Rule no. 16

We can stop the data running from a return statement of the method.

Example:

Rule no. 17

We cannot make 2 methods with the same name and the same parameter type.

Example:

Rule no. 18 (Method overloading)

We can make any no.of methods with the same name but different parameter types.

Example:

Java Methods lesson is over now. Let’s meet with the new topic “Arrays” from the next article.

Thank you.

Share this article

Keep reading