Java series: Seemingly simple problems the difference between static and instanced methods

Source: Internet
Author: User

(2011-12-06 15:28:26) reproduced
Tags: gossip Category: Study

I haven't really figured it out today. The difference between static methods and instance methods, search online, find a good article, and share with you:

This is a frequently asked questions, many times we think we understand, understand, but dig a bit, we found that do not understand.

The method is that we write every day, many programmers use the instantiation method, and seldom use static method, ask why do not say why, or simply answer the difference between the two definitions, the static method does not need new to use the instantiation method needs new to use later .... Do we really understand that?

From the actual project development, there are three ways to develop the project:

In the development project, the BLL and Dal are separated, and the DAL code is called in the BLL.

One, using static methods in the DAL, without creating an instance of direct invocation (presumably many people develop it this way)

Class DAL
{
public static string GetUserName (...);
}
Called in the BLL:
DAL. GetUserName ();

Second, use the instance method in the Dal, using the static member pattern (or singleton) to invoke by instance:
Class DAL
{
public static readonly dal dal = new Dal ();
public string GetUserName (...);
}
Called in the BLL:
DAL.dal.GetUserName ();

Third, use the instance method in the Dal to create an instance of the Dal object before each call:
Class DAL
{
public string GetUserName (...);
}
Called in the BLL:
Dal dal = new Dal ();
Dal. GetUserName ();

---------------------------------------------------------------

Development mode One: I think in some cases (such as calling multiple databases, GetUserName (...) Internal processing Operations Section) is suspected of thread safety. This type of development is not a new object, so it is common.

Development Mode Two: should be more application in CS mode, Dal in the entire project will only have an object exists, if appear in B/S I think can not be compatible with a variety of problem situations. And also the problem of thread safety.

Development method Three: should be universally used, can be compatible with a variety of problems, and will not be a thread of unsafe suspicion appeared.

In particular, the previous versions of Ms pet3.0 were adopted in mode two, and the pet3.0 and later versions were adopted in three ways, and were specifically explained in the development documentation. I think it should be considered from the compatibility, from the performance of the way two is no more than the way three really high how much.

-------------------------------------------------------------------------

I purposely "How do you understand and use static methods and instantiate methods?" "This question asks a number of programmers who are developing different languages (C, C + +, Java, C #)

Here are their answers:

Hailong says:

Common methods, and some scattered general static methods

Zhang Wei said:
There is almost no difference, if you do not need to instantiate, use static methods, if you want to be safe, use an instance method, so that other instance methods and variables can be called.

Shan says:
Static method is less useful, because he started on the instantiation, compared to the resources, of course, with the singleton mode is more useful
A lot of the use of data connections, I avoid the principle is to reduce resource consumption.

Zhang Xinbo says:
The static method means that I do not need to do a new operation on the class to which it belongs before the call, and I will use the static method primarily in the tool class.


to tell the details :
Static is a class, and an instance is an object.
One of the differences between static and instance methods is that static methods do not require attributes from the dependent class, and can be closed in this method to complete a function. Instance methods use more of the properties in the class.

Winson_ Zhang Lin says:
The biggest difference is in memory.
The static method generates memory at the beginning of the program, and the instance method generates memory in the program run.
So the static method can be called directly, the instance method must first genetic instance, through the instance calls the method, the static speed is very fast, but many will occupy the memory.
Any language is the operation of memory and disk, as to whether object-oriented, only the software layer of the problem, the bottom is the same, but the implementation method is different.
Static memory is continuous, because it is generated at the beginning of the program, and the instance is requesting a discrete space, so of course there is no static method fast,
and static memory is limited, too many programs will not start.

showlover says:
Static methods and instance methods have their own use ...

is defined as a static method, or as an instance method, also depends on the specific situation, such as the method itself and the type does not have too much relationship, can be defined as static method.

With an instance method, you need to create an instance first to invoke an instance method, while a static method does not.

In terms of performance, the static method is slightly more efficient, but it will reside in memory ...
In some cases it is advantageous to use a static method, because for static methods no matter how many instances you have,
A copy of the memory to be maintained. At the same time, some methods that do use static are more appropriate.

q.yuhen says:
This problem involves a lot of things, such as design patterns and so on. To put it simply, a static method is used to perform a full operation that is stateless, whereas an instance method is, in contrast, a part of a complete logic and needs to maintain a certain state value.
If you use static method with memory and efficiency to differentiate, Instance method goes back to the past structured programming. The fundamental starting point for using that approach is around object-oriented.

Chen Liang said:

A static method is similar to a global function, and an instance method is a method inside a class.

Summary : Everyone has a consensus on this issue: that is, the instantiation method is more used and safe, static method less use.

Sometimes we have some misunderstanding about static methods and instantiation methods.

1, we all think " static method resident memory, the instance method is not, so the static method is high efficiency but occupies memory." "

In fact, they are all the same, when loading the timing and taking up memory, the static method and the instance method are the same, loaded when the type is used for the first time. The speed of the call is basically no different.

2. Everyone thought " static method allocates memory on heap, instance method on stack "

In fact, none of the methods can allocate memory on the heap or on the stack, as the code is loaded into a special area of code memory that is not writable.

Method takes up no more memory, and it doesn't matter whether it's static or not.
Because fields are used to store information for each instance object, the fields occupy memory, and because each instance object's state is inconsistent (at least not considered consistent), all the fields of each instance object have a copy of the memory. And that's why you can use them to tell which object you're working on.
But the method is different, no matter how many instance objects, the code of the method is the same, so as long as there is a copy of the code is enough. So either the static or the Non-static method, there is only one copy of the code, that is, to occupy only a portion of memory space.
The same code, why does it behave differently? This relies on the data used by the method. There are two main sources of data, one is through the parameters of the method, and the other is to use the value of the class member variable ...

3, we all think that " instance method needs to create an instance before it can be called, more trouble, static method is not, relatively simple "

In fact, if a method has nothing to do with the instance object of his class, it should be static and should not be written as an instance method. So all the example methods are related to the instance, since it is related to the instance, then creating an instance is the inevitable step, without the hassle of simply saying it.

Of course, you can completely write all the instance methods as static, passing the instance as a parameter, in general there may not be any problem.

From an object-oriented perspective, when you choose to use an instantiated or static method, you should use a static method if you want to use an instantiated object, depending on whether the method and the instantiated object have a logical dependency. 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 divide the method area into: Static and instantiation methods?

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 is the object-oriented concept after the emergence of things, the distinction between static and instantiation methods can not only from the performance to understand, create c++,java,c# In this way, the introduction of the object-oriented language of the master is not to solve the problem of performance, memory, but in order to make development more modular, object-oriented. In this case, the static method and the instantiation mode are differentiated to solve the problem of the pattern.

Take someone else an example to say things:

For example, "people" This class, everyone has a name, age, gender, height, etc., these attributes should be non-static, because each of these attributes are not the same, but the biology of which the door which the goal of which, this attribute belongs to the whole human, so it should be static-- It does not depend on a particular person, and there is no one who is a "vertebrate mammal" and some one is "Artiodactyla".

Java series: Seemingly simple problems the difference between static and instanced methods

Related Article

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.