Classes

Now that we have looked at the actual code lines and have understood how the algorithm works, let's look at the more global structures of the code that brings it together: classes and packages enclosing the methods.

Every file in a Java program defines a class. Any code in a Java program is inside a class. There is nothing like global variables or global functions as in C, Python, Go, or other languages. Java is totally object oriented.

There can be more than one class in a single file, but usually one file is one class. Later, we will see that there are inner classes when a class is inside another class, but, for now, we will put one class into one file.

There are some features in the Java language that we do not use. When the language was created, these features seemed to be a good idea. CPU, memory, and other resources, including mediocre developers, were also more limited than today. Some of the features, perhaps, made more sense because of these environmental constraints. Sometimes, I will mention these. In the case of classes, you can put more than one class into a single file so long as only one is public. That is bad practice, and we will never do that.
Java never obsoletes these features. It is a philosophy of Java to remain compatible with all previous versions. This philosophy is good for the already written, huge amount of legacy code. Java code written and tested with an old version will work in a newer environment. At the same time, those features lure beginners to a wrong style. For this reason, sometimes, I will not even mention these features. For example, here, I could say: There is one class in a file. This would not be absolutely correct. At the same time, it is more or less pointless to explain in great detail a feature that I recommend not to be used. Later, I may simply skip them and "lie". There are not too many of those features.

A class is defined using the class keyword and each class has to have a name. The name should be unique within the package (see the next section) and has to be the same as the name of the file. A class can implement an interface or extend another class, for which we will see an example later. A class can also be abstract, final, and public. These are defined with the appropriate keywords, as you will see in examples.

Our program has two classes. Both of them are public. The public classes are accessible from anywhere. Classes that are not public are visible only inside the package. Inner and nested classes can also be private visible only inside the top-level class defined on the file level.

Classes that contain a main method to be invoked by the Java environment should be public. That is because they are invoked by the JVM.

The class starts at the beginning of the file right after the package declaration and everything between the { and } characters belong to the class. The methods, fields, inner or nested classes, and so on are part of the class. Generally, curly braces denote some block in Java. This was invented in the C language, and many languages follow this notation. Class declaration is some block, methods are defined using some block, loops, and conditional commands use blocks.

When we use the classes, we will have to create instances of classes. These instances are objects. In other words, objects are created instantiating a class. To do that, the new keyword is used in Java. When the line final Sort sorter = new Sort(); is executed in the App class, it creates a new object instantiating the Sort class. We will also say that we created a new Sort object or that the type of the object is Sort. When a new object is created, a constructor of the object is invoked. A bit sloppy, I may say, that the constructor is a special method in the class that has the same name as the class itself and has no return value. That is because it returns the created object. To be precise, constructors are not methods. They are initializers and they do not return the new object. They work on the not-ready-yet object. When a constructor executing the object is not fully initialized, some of the final fields may not be initialized and the overall initialization still can fail if the constructor throws an exception. In our example, we do not have any constructor in the code. In such a case, Java creates a default constructor that accepts no argument and does not modify the already allocated but uninitialized object. If the Java code defines an initializer, then the Java compiler does not create a default one.

A class can have many constructors, each having different parameter list.

In addition to constructors Java classes can contain initializer blocks. They are blocks on the class level, the same level as the constructor and methods. The code in these blocks is compiled into the constructors and is executed when the constructor is executing.

It is also possible to initialize static fields in static initializer blocks. These are the blocks on the top level inside the class with the static keyword in front of them. They are executed only once when the class is loaded.

We named the classes in our example App and Sort. This is a convention in Java to name almost everything in CamelCase.

CamelCase is when the words are written without spaces between them. The first word may start with lowercase or uppercase, and, to denote the start of the second and subsequent words, they start with uppercase. ForExampleThisIsALongCamelCase name.

Class names start with an uppercase letter. This is not a requirement of the language formally, but this is a convention that every programmer should follow. These coding conventions help you create code that is easier to understand by other programmers, and lead to easier maintenance. Static code analyzer tools, such as Checkstyle (http://checkstyle.sourceforge.net/), also check that the programmers follow the conventions.

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

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