When defining a class (all of our work in Java is to define classes, by making objects of those classes and sending messages to those objects, you can set two types of elements in your class: data members (sometimes called "Fields"), and member functions (usually called "methods"). Where the data member is an object (communicates with its handle), can be of any type. It can also be one of the main types (not handles). If you are pointing to a handle to an object, you must initialize that handle and connect it to an actual object with a special function named "Builder" (which is detailed in chapter 4th) (as you saw earlier, using the New keyword). However, if a primary type is available, it can be initialized directly at the class definition location (as you'll see later, the handle can also be initialized at the defined location).
Each object retains storage space for its own data member, and data members are not shared among objects. The following is an example of a class that defines some data members:
Class DataOnly {
int i;
float F;
Boolean B;
}
This class doesn't do anything substantive, but we can create an object:
DataOnly d = new dataonly ();
You can assign a value to a data member, but you must first know how to reference a member of an object. To achieve the purpose of referencing an object member, first write the name of the object handle, followed by a point number (period), followed by the name of the member within the object. "Object handle. Member". For example:
D.I = 47;
D.F = 1.1f;
D.B = false;
One object may also contain another object, and another object contains the data that we want to modify. For this problem, just keep the "connect period". For example:
myPlane.leftTank.capacity = 100;
In addition to accommodating data, the DataOnly class can no longer do more things because it does not have a member function (method). In order to understand the principle of work correctly, you must first know the concept of "independent variable" and "return value". We'll explain it in a minute.
1. Default values for primary members
If a primary data type belongs to a class member, it can be guaranteed to get a default value even if it is not explicitly (explicitly) initialized.
Main type default value
Boolean false
Char ' \u0000 ' (null)
BYTE (byte) 0
Short (short) 0
int 0
Long 0L
Float 0.0f
Double 0.0d
Once you use a variable as a class member, you should pay special attention to the default values that are assigned by Java. This ensures that the member variables of the main type are initialized (c + + does not have this feature) and can effectively deter multiple related programming errors.
However, this assurance does not apply to "local" variables--those variables are not fields of a class. So, if you write the following code in a function definition:
int x;
Then X will get some random values (this is the same as C and C + +) and will not automatically initialize to 0. It is our responsibility to allocate an appropriate value before the official use of X. If you forget, you get a compile-time error telling us that the variable may not have been initialized. This kind of processing is one of Java's superior C + + performance. Many C + + compilers warn against the initialization of variables, but in Java they are errors.