Difference and use of class method and instance method

The methods of a class in Java are divided into class methods (modified with static), also known as static methods and instance methods (not modified with static)

1. Instance method:

When the bytecode of the class is loaded into the memory, the instance method of the class is not assigned to the entry address, only after the object of the class is created, the instance method is assigned the entry address. Therefore, the instance method can be called by all objects created by the class. It is also important to note that when we create the first object of the class, the entry address of the instance method will be allocated, and when the object is subsequently created, it will not be Allocate a new entry address. All objects of this class share the entry address of the instance method. When all objects of this class are destroyed, the entry address will disappear.

2. Class methods

When the bytecode file of the class is loaded into the memory, the entry address of the class method will be allocated, so the class method can be called not only by the object of the class, but also directly through the class name. The entry address of the class method is only the program It disappears after exiting.

Because the allocation of the entry address of the class method is earlier than the allocation time of the entry address of the instance method. The object created by the class in oc does not need to be released, the object will be placed in the automatic buffer pool.

3. Example:

 

NSObject *object1 =  [[NSObject alloc]init];

 instance method starts with "-"

class method starts with "+", equivalent to static method

+(void)function;//Class method, a method that can be called without instantiating an object

- (void)function;//Member method, must be called through the instantiated object

Among them, oc is a message mechanism to transmit information, send an alloc message to allocate memory space to the class, send an init message to generate an object, and the pointer points to the object itself.

4. Summary:

(1) Instance variables cannot be referenced in class methods—the definition of instance variables is similar to instance methods, and there is no variable modified with static. The creation of instance variables is similar to the creation of instance methods, which is also completed when the class is created, so Instance variables cannot be referenced in class methods, because the instance variables have not yet been allocated memory addresses at this time.

(2) The super and this keywords cannot be used in class methods

This is because both super and this point to objects of the parent class and this class, and when the class method is called, the objects referred to may not have been created yet.

(3) Class methods cannot call instance methods

Compared with class methods, instance methods have no restrictions on the definition:

(1) Instance methods can reference class variables and instance variables

(2) Instance methods can use super and this keywords

(3) Class methods can be called in instance methods

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325046791&siteId=291194637