Fifth chapter: Initialization and cleanup
Initialization and cleanup are two issues that involve program security. The Java constructor initializes the new object while the garbage collector cleans it.
The name of the constructor must be the same as the class name, although the constructor is a special method, but the first letter lowercase encoding style for each method is not appropriate for the constructor.
The constructor does not have a return value, but the new expression returns a reference to the new object.
Method overloads are required by the constructor. Each overloaded method must have a unique parameter list. Even the difference in the order of the parameters is enough to distinguish two methods. However, this is not generally recommended because it makes the code difficult to maintain. When the incoming data type is less than the formal parameter type declared in the method, the actual data type is promoted. However, the Boolean cannot be converted, and Char is also more special, promoted to int. If in turn, the incoming is greater than the declaration, you must make a strong turn, or an error. (Note that it is not true that the method is differentiated based on the return value.) )
Each parameter type is arranged in the order of size: Boolean:1/8;byte:1;char:2 (one character can store a Chinese character); Short:2;int:4;float:4;long:8;double:8. (Unit: bytes)
This keyword: can only be used inside a method, representing a reference to the "object that called the method". Use this only if you need to explicitly indicate a reference to the current object. If you use another method within the same class inside a method, you do not have to use this. Return this means that the current reference is returned.
This is usually written as "this object" or "that object", and it itself represents a reference to the current object. In the constructor, if this adds a parameter list, it has a different meaning. This results in an explicit call to a constructor that conforms to this parameter list. The following code example will understand more deeply.
public class Flower {int petalcount = 0; String s = "initial value"; Flower (int petals) {petalcount = petals; System.out.println ("Constructor w/int arg only,petalcount=" + petalcount);} Flower (String ss) {System.out.println ("Constructor w/string arg only,s=" + ss); s = SS;} Flower (String s,int petals) {This (petals);
This (s)//error, cannot call two constructors in a constructor THIS.S = s;//because the name of the member and parameter s are the same, all are distinguished by this notation. System.out.println ("String & int args");} Flower () {This ("hi", 47); System.out.println ("default constructor (no args)");} void Printpetalcount () {
This (11);//error, cannot call the constructor through this in the non-constructor method. System.out.println ("Petalcount =" + Petalcount + "s=" + s);} public static void Main (string[] args) {Flower x = new Flower (); X.printpetalcount ();}}
Output
Constructor w/int arg only,petalcount=47string & int Argsdefault Constructor (no args) Petalcount =47 S=hi
Additions to Static:
The static method cannot contain this, that is, a non-static method cannot be called. In turn, yes. And when no object is created, it is possible to invoke the static method by relying on the object itself alone. This is actually the main function of static. This is much like a global approach, where global methods are forbidden in Java, but you can access other static methods and static fields by placing the static method in the class.
Some people think that static methods are not "object-oriented" because they do have the meaning of global functions, and when using the static method, they do not send information to the object through this. Indeed, if there is a lot of static in the code, you should reconsider your design. However, the concept of static has its practical, many times to use it, as to whether it is object-oriented, we do not decide.
For end-of-process and garbage collection, this will be explained in detail later in blog post.
Java strives to ensure that all variables must be properly initialized before they are used.
For basic members of a class (that is, fields) are basic types, these members are automatically initialized. However, the local variables in the method do not, you must force the programmer to give the initial value, or compile an error.
Three cases in which member initialization is initiated:
(1) The basic member variable in a class: assigns it directly to the defined place. (not available in C + +)
(2) Non-basic types of objects:
Class Depth{}public class Measurement {Depth d = new Depth ();}
(3) by invoking a method to provide the initial value, this method can also have parameters, but these parameters must be the occasional initialization:
Public CALSS Methodinit2{int i = f (); int j = g (i); int f () {return 11;} int g (int n) {return n * 10;}}
Thinking in Java Chapter Fifth learning notes