(One appropriate use might be to count the number of objects instantiated from a specific class. I suspect there are a few other appropriate uses as well.)Static methods
Don't declare methods static if there is any requirement for the method to remember anything from one invocation to the next.
There are many appropriate uses for static methods, but in most cases, the purpose of the method will be to completely perform some action with no requirement to remember anything from that invocation to the next.
The method should probably also be self-contained. By this I mean that all information that the method needs to do its job should either come from incoming parameters or from final static member variables (constants). The method probably should not depend on the values stored in non-final static member variables, which are subject to change over time.
(A static method only has access to other static members of the class, so it cannot depend on instance variables defined in the class.)An appropriate example of a static method is the sqrt method of the Math class. This method computes and "Returns the correctly rounded positive square root of a double" where the double value is provided as a parameter to the method. Each time the method is invoked, it completes its task and doesn't attempt to save any values from that invocation to the next. Furthermore, it gets all the information that it needs to do its job from an incoming parameter.
Summary
The existence of static members tends to break up the simple OOP structures that I have discussed in previous lessons in this miniseries.
While static members can be useful in some situations, the existence of static members tends to complicate the overall object-oriented structure of Java.
Furthermore, the overuse of static members can lead to problems similar to those experienced in languages like C and C that support global variables and global functions.
The class named Class
I discussed the class named Class and how a conceptual object of type Class exists in memory following a reference to a specific class in the program code.
The Class object represents the referenced class in memory, and contains the static variables and static methods belonging to that class. (It contains some other information as well, such as the name of the superclass.)
Class members and instance members
Class variables and class methods are declared static (declaring a member static in the class definition causes to be called a class member).
Instance variables and instance methods are not declared static.
Each object has its own copy ...
Every object instantiated from a given class has its own copy of each instance variable declared in the class definition. (Instance variables are not shared among objects.)
Every object instantiated from a given class acts like it has its own copy of every instance method declared in the class definition. (Although instance methods are actually shared among objects in order to reduce the amount of memory required, they are shared in such a way that they don't appear to be shared.)
Every object shares ...
Every object instantiated from a given class shares the same single copy of each class variable declared in the class definition. Similarly, every object instantiated from a given class shares the same copy of each class method.
Accessing an instance member
An instance variable or an instance method can only be accessed by using a reference to the object that owns it. Even then, it may or may not be accessible depending on the access modifier assigned by the programmer.
Accessing a class member
The single shared copy of a class variable or a class method can be accessed in either of two ways:
- Via a reference to any object instantiated from the class
- By simply joining the name of the class to the name of the class variable or the class method
When to use class variables
It is very often appropriate to use final static variables, as constants in your programs. It is rarely, if ever, appropriate to use non-final static variables in your programs. The use of non-final static variables should definitely be minimized.
When to use static methods
It is often appropriate to use static methods in your programs, provided there is no requirement for the method to remember anything from one invocation to the next. Static methods should be self-contained.
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




