This chapter is really the beginning of OOP. I think Java is really designed with the concept of OOP. Even those process oriented programming elements are forced to be absorbed in that systems and become static element. If in one day, which may not be too far away, OOP are no longer popular, Java would either.
Ironically, we always had to have some place to start up a program, and that place can’t be an non-static method, at which no object has been created since it is the beginning. So, the starting point is always a static method, either main() method or something else in JUnit or things like that. In that static method, we can call other static methods or create new objects and run non-static method on that. However, every static method cannot go beyond classes.
Yesterday, one of my friends asked me questions about calling a method in another methods. I hadn’t think into this problem systemically, and hence I summarized all situations: in a static method, you can call other static methods in same class and omit class name, call other classes’s static methods, and call non-static methods via objectname.method; in a non-static method, in additional to the above, you can also call method of this object omitting “this.”. My friend said that the reason he was confused is that an omitted prefix can be either “classname.” or “this.”. I told him, force POP into Java’s OOP system as static fields is what the problem is.
According to the book, how non-static method works is Java inwardly take the object as an additional parameter of the method. Actually, python also do this, but in public: first parameter must be “self ” in this case.
And it also solved a doubt: why some method return this, since this should be known when called; in this case, multiple operations can be operated in one line.
“this” has another use: use this(paras…) as constructor in other overloading constructors, but only once at the beginning of other constructor, and must be at the very beginning, like “super”.
It is not a surprise that we cannot expect finalize() to run since people don’t know when garbage collection work. The book point out that two situations where finalize() is needed: one is when you are using JNI since C/C++ don’t have GC systems. The other is to verify the ending condition.
Other parts of the class is about initialization. Basically, programmers must manually initialize local variables. For fields, things are more flexible. It has a default value. Also, the value can be set by programmer. Basically, those initialization is by order, which means methods which has been defined can be called. However, even if fields are separated by methods, all of them will be initialize. When a class have static fields and blocks, they will always be initialized first when the containing class are referred, either static or non-static. However, those initializations only run one time. Then, every time an instance are created, non-static fields and blocks will be initialized. After that, the constructor runs.