What does the expression
account1.accountNumber
mean?
The expression means to use the variable accountNumber
that is part of the object referred to by account1
.
The dot operator is how you access a member of an object:
referenceToAnObject . partOfTheObject
Of course, an object has to exist, and there has to be a reference to it. In the program, this happened when the object was constructed:
CheckingAccount account1 = new CheckingAccount( "123", "Bob", 100 );
When a method has a reference to an object, the method can:
However, it is common for a class to
have private
variables and methods.
Only methods of the class can access these.
This will be discussed further in the next chapter.
(Thought Question: ) Do you think that the following would work?
class CheckingAccountTester { public static void main( String[] args ) { CheckingAccount account1 = new CheckingAccount( "123", "Bob", 100 ); account1.balance = 10000000; } }