Changes

Jump to: navigation, search
no edit summary
account1.accountBalance = 6789.98;
</pre>
 
A key point to understand about dot notation is that it only works for instance variables for which synthesized accessor methods have been declared. If you attempt to use dot notation to access an instance variable for which no synthesized accessor is available the code will fail to compile.
 
== Controlling Access to Instance Variables ==
 
By default, instance variables in a class can be accessed by the methods of that class. This is the default for instance variables and is called ''protected access''. In addition to ''protected access'', a number of other options are available:
 
* '''protected''' - Access is allowed only by methods of the class and any subclasses.
 
* '''private''' - Access is restricted to methods of the class. Access is not available to subclasses.
 
* '''public''' - Direct access available to methods of the class, subclasses and code in other module files and classes.
 
The access level for instance variables is specified in the @interface section of the class declaration using the ''@protected'', ''@private'' and ''@public'' directives. Once a directive has been specified, all instance variables beneath that line adopt that setting until another directive is specified. For example:
 
<pre>
@interface
{
double accountBalance;
@private
long accountNumber;
int accessCount;
@public
float interestRate;
}
 
In the above example, ''accountbalance'' will default to ''protected access'' since it is not preceded by a directive, ''accountNumber'' and ''accessCount'' are both ''private'' and ''interestRate'' is public.
 
When accessing a public instance variable from another class or any other code in a methods or function, the -> pointer operator notation is used.
 
For example:
 
<pre>
account1->accountBalance = 12345.67;
</pre>
 
An attempt to access a non-public instance variable using pointer notation will result in a compiler warning similar to the following:
 
<tt>warning: instance variable 'accountNumber' is @protected; this will be a hard error in the future</tt>

Navigation menu