"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 Category (Class) The internal composition is collectively referred to as a member, and if the member is "information" or "program", it can be divided into:
According to the Field program, it is called Method if it is divided according to the use of static modifications, the members can be divided into four types:
Class Field: Useful Static Modification Field Class Method: Useful for modhod instance field: Field Instance Method modified with static: No use of Static modified Method as the name, Class Field / Method and Class itself have close relationship, but instance Field / Method has a close relationship with 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 () {// ...}}
When Field declares Field, if you add Static's modifier 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. When Method declares 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 ().
The hidden THIS parameters are integrated above the narrative:
Class Field: Shared a memory class method: Shared a memory instance field: With each instance each memory instance instance method: shared one memory instance method why not with each instance accounts, Sharing a memory? 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.instancethod ();
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.