The Essence of OOP using Java: Static Members…
2008-02-23 09:45:59来源:互联网 阅读 ()
instance variable, and not declaring a method to be static causes it to be an instance method. In general, we can refer to them as class members and instance members.
What is the difference?
Here are some of the differences between class and instance members insofar as this discussion is concerned.
How many copies of member variables exist?
Every object instantiated from a given class has its own copy of each instance variable defined in the class.
(Instance variables are not shared among objects.)However, every object instantiated from a given class shares the same copy of each class variable defined in the class.
(It is as though the class variable belongs to the single Class object and not to the individual objects instantiated from that class.)Access to an instance variable
Every object has its own copy of each instance variable (the object owns the instance variable). Therefore, the only way that you can access an instance variable is to use that object's reference to send a message to the object requesting access to the variable (even then, you may not be given access, depending on access modifiers).
Why call it an instance variable?
According to the current jargon, an object is an instance of a class.
(I probably told you that somewhere before in this miniseries. At least, I hope that I did. After writing eight or ten lessons in a miniseries, I sometimes forget what I told you before.)Each object has its own copy of each non-static variable. Hence, they are often called instance variables. (Every instance of the class has one.)
Access to a class variable
You can also send a message to an object requesting access to a class variable that the object shares with other objects instantiated from the same class. (Again, you may or may not gain access, depending the access modifiers).
Access using the Class object
More importantly, you can also access a class variable without a requirement to go through an object instantiated from the class. (In fact, a class variable can be accessed in the total absence of objects of that class.)
(Remember, this discussion is conceptual in nature, and may not represent an actual implementation.)Assuming that a class variable is otherwise accessible, you can access the class variable by sending an access request message to the Class object to which the variable belongs.
One way to think of this
To help you keep track of thing in a message-passing sense, you can pretend that there is a global reference variable whose name is the same as the name of a class.
This (hypothetical) reference variable contains a reference to the Class object that owns the class variable. Using standard Java message-passing syntax, you can access the class variable by joining the name of the reference variable to the name of the class variable with a period. Example syntax is shown below:
ReferenceVariableName.ClassVariableName
As a result of the hypothetical substitution process that I described above, this is equivalent to the following:
ClassName.ClassVariableName
We will see an example of this in the sample program that I will discuss later.
Be careful with this thought processCharacteristics of class methodsWhile this thought process may be useful when thinking about static variables and methods, I want to point out, that the thought process breaks down very quickly when dealing with Class objects in a deeper sense.
For example, when invoking the getName method on a Class object, an actual reference of type Class is required to access the members of the Class object. The name of the class will not suffice.
If this discussion of a global reference variable whose name matches the name of the class is confusing to you, just forget it. Simply remember that you can access class variables by joining the name of the class to the name of the class variable using a period as the joining operator.
I'm not going to talk very much about instance methods and class methods in this lesson. However, there are a couple of characteristics of class methods that deserve a brief discussion in this context.
Cannot access instance members
First, the code in a class method has direct access only to other static members of the class.
标签:
版权申明:本站文章部分自网络,如有侵权,请联系: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
