"After a Class has an instance, all instance fields of this class will add one, then do all instance method will add a copy?" I often see someone on the Internet discussed this issue, so write This article thought it was explained.
MEMBER type
The internal composition of the class (Class) is a member (Member), and if the member is "information" or "program", it can be divided into:
Information, known as FIELD
Program, known as Method
If it is divided according to the use of Static modification, the member can be divided into four types:
Class Field: Useful with static modified field
Class method: useful with static modified Method
Instance field: No static modified field
Instance Method: No static modified Method
As the name suggests, Class Field / Method and Class itself have close relationships, while instance field / method is closely related to Instance (which is the object). Please see the example of the following.
Public class class1 {
Public Static Int Classfield;
Public static void classmethod1 () {
// ...
}
Public static void classmethod2 () {
// ...
}
Public int instancefield;
Public void instanceMethod1 () {
// ...
}
Public void instanceMethod2 () {
// ...
}
}
Public class class2 {
Public static void classmethod () {
// ...
}
Public void instanceMethod () {
// ...
}
}
Field
When decrees FIELD, if you add Static's modified words, this Field will become a Class Field. A Class Field will only take only one memory, and this memory space is configured immediately after this Class is loaded (LOAD) memory, it feels like this Field is related to the Class itself, not Related to the Class's Instance. Class Field can be used directly inside the same class of Class Method, for example, the ClassField can appear inside the ClassMethod1 () described above. If Class1's Class Method or instance method wants to use Class2's Class Field, you must top Class2 in front, for example: Class2.classField.
When declaring Field, if you add static modifier words, this Field will become Instance Field. For Instance Field, each instance field memory is generated, each instance is less, with a small INSTANCE Memory of the INSTANCE. Instance Field can be used directly within the INSTANCE METHOD of Instance Method, an instancefield can appear inside the INSTANCETHOD1 () described above. If a Class Class Method or instance method wants to use instance field, you must top the Instance name in front, for example: obj.classfield. Method
When declared Method, if you add Static's modified words, this Method becomes Class Method. For Class Method, only one memory is always occupied, and this memory space is configured immediately after this Class is loaded into the memory, as this method is related to the Class itself, not to Class's Instance is related. Class Method can be used directly in any Class Method in the same class, and ClassMethod2 () can occur inside the above ClassMethod1 (). If Class1 Class Method or instance method wants to use Class2's classMethod (), you must top Class2 in front, which is class2.classmethod ().
When declaring Method, if you add static modified words, this Method will become Instance Method. For Instance Method, each instance method memory is generated every INSTANCE. The same Method regardless of the invoke, no matter what the instance is called, each time the code is completely, the difference is only different at each execution, and the information is stored in the Call Stack, So will not be confused. In Instance Method, the source of information includes parameters and instance field. The parameters are passed into the entry in the Call Stack, so it will not be confused, which is easy to understand, but the instance field is interrupted (just mentioned before: Instance Field increases with the number of instances), This is achieved through the THIS parameters of the implicitit, and will be explained later. Instance Method can be used directly inside the INSTANCE METHOD, for example, INSTANCEMETHOD2 () can occur inside INSTANCEMETHOD2 (). If a Class's Class Method or Instance Method wants to use a instance method for a instance, you must have this Instance name in front, for example: obj.classmethod ().
Hidden this parameters
Synthesize the above description:
Class Field: Shared a memory
Class method: Shared a memory
Instance Field: With each instance each has a memory instance method method: shared a memory
Why is INSTANCE METHOD not with a memory with a memory of each instance? In fact, let each instance method are like instance field, as each instance has a memory, which is of course ok, just the Java compiler and JVM don't do this, because it is too waste of memory space. A Field is less occupying a BYTE, which takes hundreds of Byte, but Method will have a few byte, and there are hundreds of KILO BYTE. Mehtod consumes hundreds of times, or even thousands of times, of course, as far as possible, compares to consume memory. Since JVM let a class of all instance share the same Instance Method, how to distinguish between instance1 or instance2 when the following two row codes are in instancethod ().
Instance1.instanceMethod ();
Instance2.instanceMethod ();
Because the compiler will help us use the initiation1 and instance2 individuals as the first parameter. That is, the actual number of any instance method parameters will be more than one, and the more parameters are used by the Java compiler to help us with the corresponding instance. The variable name of this parameter is this, which is also a keyword of Java (Keyword).
When calling an instance method or when using an instance field, you must add the name of the instance in front, if the instance method / field-related instance and the instance method of the instance method are in the same instance, When the instance of the instance is this, in which case you can also choose not to add "this.".
However, in some cases, it is not possible to add "this." In front. For example, when the parameter or zone variable in Method is completely identical, if not on the top "this.", Then the parameter or zone variable; if it is crowning "THIS.", Then It is Instance Field.