Static methods vs Non-static methods

Source: Internet
Author: User

                            static methods vs Non-static methods

My understanding: Static and non-static methods are designed at the beginning of the first goal is the former is for the class, that is, a certain

Objects are owned by the collective , and not static methods are targeted at an object . When you reference a static method, you can use the

The object name is prefixed, and the class name can be prefixed with it.

A few things programmers need to focus on

Static methods

Non-static methods

Call procedure

Class name. Method Name ()

The name of the object. Method Name ()

Life cycle

Operating cycle of the entire program

The life cycle of an object

Memory resident

Resident memory

Memory Not resident

Frequency of Use

Use with caution

Recommended

Programmers don't need to focus on the points

Static methods

Non-static methods

Overwrite (overWrite)

Allow Quilt class overrides

Allow Quilt class overrides

How it exists in memory

Baidu a bit

Baidu a bit

The above points are mixed into the idea of object-oriented, if you want to drill down, there may be problems, but for

It's enough for us programmers to understand that.


First, static variables:
The principle is to share data segments;
as long as there is no "write" operation will not cause problems, but the data is usually used for reading and writing, so static variables to pay attention to use; Here is a simple example of using a static method:


Operation result is: 8


We all have a consensus on this issue: the instantiation method is more used and secure, and the static method is less used. Sometimes we have some misunderstanding about static methods and instantiation methods.
1, we all thought that "static method resident memory, instance method is not, so the static method is high efficiency but occupies the internal

storage. " In fact, they are all the same, when loading the timing and taking up memory, static methods and instance methods are the same

of, in The type is loaded the first time it is used. The speed of the call is basically no different.


2. everyone thought "static method allocates memory on heap, instance method on stack" in fact all methods are

No memory may be allocated on the heap or on the stack, as the code is loaded into a special area of code memory, which

inside The storage area is not writable.    method takes up no more memory, and it doesn't matter whether it's static or not.

Because a field is used to store information for each instance object, the field occupies memory, and because each real

Example the state of the object is inconsistent (at least not considered consistent), so every field of the instance object

will have a copy in memory, so that you can use them to differentiate between what you're working on

like. But the method is different, no matter how many instance objects, the code of the method is the same, so long as there are

One piece of code is enough. So either the static or the Non-static method, There is only one copy of the code, which is

occupies only one portion of memory space. The same code, why does it behave differently? This is dependent on the method

The data that was used. There are two main sources of data, one is through the parameters of the method is passed in, the other is to make

The value of the member variable with class ...


3, We all thought that "the instance method needs to create the instance first can call, compares the trouble, the static method does not have,

Very simple "
In fact, if a method has nothing to do with the instance object of his class, then it should be static and should not

Write it as an example method. So all the instance methods are related to the instance, and since it is related to the instance, creating the actual

Example is the inevitable step, no trouble simple to say. Of course, you can completely write all the instance methods into static

, the instance is passed in as a parameter, and in general it may not be a problem.
From an object-oriented perspective, when choosing to use an instantiation or static method, it should be based on whether the party

and instantiated objects have a logical correlation, and if so, you should use an instance use static side instead of the object

Method. This is only from an object-oriented perspective.

If it is from the thread safety, performance, compatibility is also the choice of instantiation method is advisable. Why do we have to put the party

The law area is divided into: static method and instantiation method?
If we continue to study in depth, we must leave the technology to talk about theory. Early structured programming, almost all

Methods are "static methods", the introduction of the concept of instantiation method is the object-oriented concept after the emergence of things, to distinguish

Static and instantiation methods cannot be understood solely from performance, creating object-oriented languages such as c++,java,c#

Master Introduction Instantiation method must not be to solve what performance, memory problems, but to let development more mode

and object-oriented. In this case, the static method and the instantiation mode are differentiated to solve the problem of the pattern.

Contact Life Example:
People, for example, have names, age, gender, height, and so on, and these attributes should be non-static

, because each of these attributes is not the same, but the biology of which the door which is which the goal,

This attribute belongs to the entire human being, so it should be static--it does not depend on a particular person, and there is no

Some people are "vertebrate mammals" and some people are "Artiodactyla".

The method is only related to the class, not to the instance, there is one exception, that is, polymorphism no matter how many instances, the method has only one copy static method and non-static method does not have any difference instance data is one copy of each object, put in the heap

and note that it is placed in the data area and the method is put in the code area method only a copy, not only save space, also body

The object-oriented principle is now available. Object has a common behavior, which is also one of the important characteristics of the object, don't you think

It's also a good representation of the face to the principle of the object?


static method What is wrong to understand!
The address is generated at compile time, and the call address is used to implement the function invocation (or similar

's instructions)

Since some non-static methods cannot be determined at compile time, it is necessary to dynamically +vmt the table based on the address of the object.

form to calculate the address of the function, and then call

When the compiler is statically linked, the address of the function is determined by the compiler in the process space.

Out of a fixed offset value ~
Each thread creates a stand-alone execution environment when executing a static method, and there are multiple static methods in memory that "actually

Example "is executed at the same time. Therefore, as long as the method does not involve sharing data, it is not possible for multiple threads to call static parties at the same time

The return result may be inaccurate. Whether a static method or an instance method is a table of type methods

To find, and then go to execute, each call creates a new temporary stack, as long as the calling code is the same, how

may be effective rate, if it is, it may be the time to instantiate the object is also counted!



Comparison Summary:
1, static members belong to the class all, non-static members belong to the instance of the class all.

2, the static method does not need new to be able to use the instantiation method need to be new before you can use

3. Non-static methods can access both static data members and non-static data members, while static methods can only

Access to static data members; Similarly, non-static methods can access both static and non-static methods, and

Quiet only static data methods can be accessed by the State method. In particular, the main method in the static method, which acts as a program

The entry point of the line, which is available in each program and only can have one.


4, each instance of creating a class, will be in memory for non-static members of the new allocation of a piece of storage;


5. Non-static Members are class-owned, common to instances of each class, regardless of how many instances the class creates, the static of the class

Members occupy only the same area in memory.

Summary of experience: use and soundness of instantiation methods, less use of static methods

Read more click me!








Static methods vs Non-static methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.