The Essence of OOP using Java: Static Members…
2008-02-23 09:45:59来源:互联网 阅读 ()
(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.
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
