Hello buddies , today we will learn how to design methods and understand their return type. We will explain them in Java Programming Language.
Every interesting Java program we’ve seen has had a main() method. We can write other methods,too. For example, we can write a basic method to take a nap.
This is called a method declaration, which specifies all the information needed to call
the method. There are a lot of parts.Let’s explain all parts of this method :
1- public -> an access modifier
Java offers four choices of access modifier:
public : The method can be called from any class.
private : The method can only be called from within the same class.
protected : The method can only be called from classes in the same package or subclasses.
Default (Package Private) Access : The method can only be called from classes in the same package. This one is tricky because there is no keyword for default access. You simply omit the access modifier.
2- final -> optional specifier
There are a number of optional specifiers. Optional
specifiers come from the following list. Unlike with access modifiers, you can have multiple
specifiers in the same method (although not all combinations are legal). When this happens, you can specify them in any order. And since it is optional, you can’t have any of them at all. This means you can have zero or more specifiers in a method declaration.
static : Used for class methods.
abstract : Used when not providing a method body.
final : Used when a method is not allowed to be overridden by a
native : Used when interacting with code written in
another language such as C++.
strictfp : Used for making floating-point calculations portable.
3 – void -> the return type of the method
The return type might be an actual Java type such as String or int. If there is no return type, the void keyword is used. This special return type comes from the English language: void means without contents. Therefore, in Java, we have no type there.
When checking return types, most importantly, you also have to look inside the method body. Methods with a return type other than void must have a return statement inside the
method body. This return statement must include the primitive or object to be returned.
Methods that have a return type of void are permitted to have a return statement with no
value returned or omit the return statement entirely.
Some examples. Checking if the method will compile of not.
Since the return type of walk1() is void, therefore the return statement is optional. walk2() shows the optional return statement that correctly doesn’t return anything. walk3() is a valid method with a String return type and a return statement that returns a String. walk4() doesn’t compile because the return statement is missing. walk5() doesn’t compile because the return type is missing. walk6() is a little tricky. There is a return statement, but it doesn’t always run. If a is 6, the return statement doesn’t execute. Since it should return a String, the compiler complains.
4 – nap -> the method name
You can name the method whatever you want, it is up to you.
5- (int minutes) -> the list of parameters.
Here the parentheses are requires, but we can add how many parameters we want. Or we can leave the method without parameters.
These concepts are very important when programming with Java. Hope you enjoyed.
Stay tuned for more.