The Essence of OOP using Java: Static Members…
2008-02-23 09:45:59来源:互联网 阅读 ()
ClassName@HexHashCode
Overridden version of toString method
If the class from which the object was instantiated (or some superclass of that class) contains an overridden version of the toString method, runtime polymorphism kicks in and the overridden version of the method is executed to produce the textual representation of the object.
The Date class overrides toString
In the case of this sample program, the object was instantiated from the Date class. The Date class does override the toString method.
When the overridden toString method is invoked on a Date object's reference, the String returned by the method looks something like that shown in Figure 6.
Mon Sep 17 09:52:27 CDT 2001 Figure 6
You will recall that this is the output that was produced by the code shown in Listing 8 and Listing 12.
More than you ever wanted to know ...
And that is probably more than you ever wanted to know about the expression System.out.println....
It is also probably more than you ever wanted to know about class variables, class methods, instance variables, and instance methods.
Some cautions
Before leaving this topic, I do want to express some cautions. Basically, I want to suggest that you use static members very sparingly, if at all. Several good sets of guidelines have been published regarding the use of static members. As of this writing, you can find one of them, written by Mark L. Fussell, at the following URL.
Guidelines for use of static members
I will try to summarize the general sense of most published guidelines in a few words.
Static variables
To begin with, don't ever use static variables without declaring them final unless you understand exactly why you are declaring them static.
(Static final variables, or constants, are often very appropriate. See the fields in the Color class for example.)I can only think of a few situations where the use of a non-final static variable might be appropriate.
(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.)
标签:
版权申明:本站文章部分自网络,如有侵权,请联系: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
