Class method, also known as static method, refers to the method modified with the static keyword.
There are three calling methods: direct call, class name, method name, Object Name, and method name.
The instance method is a method without modifying the static keyword.
The call method can only be the object name. method name.
When you need to access the member variables of an instance, this method is defined as an instance method.
I. Static methods are resident in memory, and the instance method is not. Therefore, static methods are highly efficient but occupy the memory.
In fact, the methods are the same. In terms of loading time and memory usage, the static method is the same as the instance method, and is loaded when the type is used for the first time. There is basically no difference in the call speed.
2. The static method allocates memory on the stack, and the instance method is on the stack.
In fact, it is impossible for all methods to allocate memory on the heap or stack. As code, the method is loaded to a special code memory area, which cannot be written.
3. instance methods can be called only after an instance is created. Static methods are not needed and simple.
In fact, if a method has nothing to do with its instance type, it should be static, and no one will write it as an instance method. So all the instance methods are related to the instance. Since it is related to the instance, creating the instance is required.
However, there is no trouble to briefly describe the steps. In practice, you can write all the instance methods as static ones, and pass the instance as a parameter.
Some methods seem unrelated to the instance, such as icomparer. compare method, but in fact every class that implements this interface will only be responsible for comparing its own type instances. This is C #1. there is no historical legacy caused by generics in the X specification.
Problem.
Most static methods are related to the instance of the class, such as various parse methods. The reason why the static method is made is that it does not have an instance as a parameter. Most of the rest are for semantic or other purposes.
The first method is to declare the instance and call the instance method.
When a class has multiple instances, such as student A, student B, and student C, we use the first method. In the case of multiple threads, as long as each thread creates its own instance, the first method
It is usually thread-safe.
The second method is to call the instance method through a static instance.
This situation is special. It is usually the only instance of this class in the whole program. We change the status of this instance by calling the instance method of this instance. This instance is thread-safe in the case of multiple threads. Unless
We lock this instance. Prevent other threads from accessing the instance.
Method 3: directly call the static method
In this case, the static method does not need to change the status of an instance. You only need to obtain a few parameters to complete the tasks. For example, to determine whether a file exists, you only need to provide the file path and file name to know whether the file exists.
Reprinted 2:
To call a static method of a class, do you need release? (Memory Management) (interview questions)
In my opinion, static member methods do not require objects or release.
Note: in fact, the static member method also has an object, called a Class Object. However, this is the first time that the system loads the static member into the memory, in addition, such objects are released only when the program is closed, and are not controlled by the program itself.
There are three concepts to distinguish: Class, instance and object, the class itself is loaded into the memory (may be the memory of the code segment, not the heap memory, or the stack memory), can be called an object; creates an instance of a class, which can be called a pair
So there is a distinction between Class Object and instance object. Class Object is a drawing, and the instance object is a building created by class object. It is actually something that exists, so it is called an object, but it has different properties. One is a drawing, and the other is an object.
A building is made out of drawings.
In fact, I also want to know which company's interview questions are not rigorous.
In this sentence, there can be two types of understanding: one is whether the objects returned in the static method of the class need to be release; the other is whether the instance method needs to be called to be release? That is to say, to call release, you must first alloc + init before calling the real
If the method of the example object is used up and release is required, directly call the static method of the class and whether the release class object is required.
From the above analysis, we can draw the following conclusion: the company has just got in touch with Objectiv-C. The interviewer of the company is not really familiar with objective-C, and is also the first guy to get started. Finally, it may be because the interviewer is wrong, or this post asks a question.
Wrong.
Reprinted 3:
I want to mark this good post. Today, the static method didn't respond at once. You have helped me a lot, I was inspired by the phrase "No need for release for static methods because there are no objects". Sometimes I learned more and easily forgot the most basic idea.
Note: The same is true. In the early years, whether it was learning C ++, objective-C, or Java, it took a long time to get used to something, I don't know what it means, so it's easy to digest it.
Class method, instance method, object method, static method, dynamic method