Methods

We have already discussed methods, but not in detail, and there are still some aspects that we should meet before we go on.

There are two methods in the sample classes. There can be many methods in a class. Method names are also camel cased by convention, and the name starts with a lowercase letter, as opposed to classes. Methods may return a value. If a method returns a value, the method has to declare the type of the value it returns and, in that case, any execution of the code has to finish with a return statement. The return statement has an expression after the keyword, which is evaluated when the method is executed and is returned by the method. It is good practice to have only one single return from a method but, in some simple cases, breaking that coding convention may be forgiven. The compiler checks the possible method execution paths, and it is a compile-time error if some of the paths do not return a value.

When a method does not return any value, it has to be declared to be void. This is a special type that means no value. Methods that are void, such as the public static void main method, may simply miss the return statement and just end. If there is a return statement, there is no place for any expression defining a return value after the return keyword. Again, this is a coding convention to not use the return statement in case of a method that does not return any value, but in some coding patterns, this may not be followed.

Methods can be private, protected, public, and static, and we will discuss their meaning later.

We have seen that the main method that was invoked when the program started is a static method. Such a method belongs to the class and can be invoked without having any instance of the class. Static methods are declared with the static modifier, and they cannot access any field or method that is not static.

In our example, the sort method is not static, but as it does not access any field and does not call any non-static method (as a matter of fact, it does not call any method at all), it could just as well be static. If we change the declaration of the method to public static void sort(String[] names) { (note the word static), the program still works, but the IDE will give a warning while editing, for example:

    Static member 'packt.java9.by.example.stringsort.Sort.sort(java.lang.String[])' accessed via instance reference

That is because you can access the method without an instance directly through the name of the Sort.sort(actualNames); class without the need of the sorter variable. Calling a static method via an instance variable is possible in Java (again something that seemed to be a good idea at the genesis of Java, but is probably not), but it may mislead the reader of the code into thinking that the method is an instance method.

Making the sort method static, the main method can be as follows:

public static void main(String[] args) { 
String[] actualNames = new String[]{
"Johnson", "Wilson",
"Wilkinson", "Abraham", "Dagobert"
};
Sort.sort(actualNames);
for (final String name : actualNames) {
System.out.println(name);
}
}

It seems to be much simpler (it is), and, in case the method does not use any field, you may think that there is no reason to make a method non-static. During the first ten years of Java, static methods were in heavy use. There is even a term, utility class, which means a class that has only static methods and should not be instantiated. With the advent of Inversion of Control containers, we tend to use less static methods. When static methods are used, it is harder to use dependency injection, and it is also more difficult to create tests. We will discuss these advanced topics in the next few chapters. For now, you are informed as to what static methods are and that they can be used; however, usually, unless there is a very special need for them, we will avoid them.

Later, we will look at how classes are implemented in the hierarchy, and how classes may implement interfaces and extend other classes. When these features are looked at, we will see that there are so-called abstract classes that may contain abstract methods. These methods have the  abstract modifier, and they are not defined—only the name, argument types (and names), and return type are specified. A concrete (non-abstract) class extending the abstract class should define them.

The opposite of abstract method is the final method declared with the final modifier. A final method cannot be overridden in subclasses.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.162.37