<< Chapter < Page Chapter >> Page >

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 theobject 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 collection.) Each object has its own copy of each non-static variable. Hence, they are often called instance variables. (Every instance of the class owns one and they are not implicitly shared among instances.)

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 sameclass. (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 things in a message-passing sense, you can pretend that there is a global reference variable whose name is the same as the name ofa 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 ofthe 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 process

While this thought process may be useful when thinking about static variables and methods, I want to point out, that the thought process breaks downvery quickly when dealing with Class objects in a deeper sense.

For example, when calling 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 youcan access class variables by joining the name of the class to the name of the class variable using a period as the joining operator.

Characteristics of class methods

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask