- private Account account is an instance variable.
- The method public Account getAccount() returns the reference of account instance and setAccount sets the reference of objects account variable to the parameter. The account instance variable cannot be accessed directly from outside as it has been declared private.
This is one of the way of data encapsulation. Instance variable are protected from unauthorized access.
- To use the getAccount() and setAccount(Account acct) methods, declare a new object of type Customer: Customer cust = new Customer(John, Willis); Use cust object to access these methods. Account acct = cust.getAccount(); cust.setAccount(acct); // you can also create a new Account object pass // it as parameter.
Please use the available CODE tags and be sure to indent when posting code segments.
The getAccount method is a typical 'getter' to retrieve some instance variable, in this case the account variable that is a reference to an Account object.
Similarly, setAccount is a 'setter' to allow you to add / set the account reference.
The idea is that you should give as little direct access possible to any instance variables by declaring them private (which you've done), and creating a set of methods to access each of those private members.
As far as your question 'how do I use it in my main method' - that's up to you. I would assume your application is going to instantiate a Customer object and in turn, an Account object that relates to that Customer.
Joined: Dec 10, 2008
thanks but if i wanna access a method in the Account class(from my main) then do i use the object of the Customer class (which does not extend to Account) or the account class itself, if i am doing so then what's the point of declaring an Account instance in Customer [ December 14, 2008: Message edited by: Vinay C ]
The Customer class has an Account associated with it, and different customers will have different accounts. So the Customer class needs a reference to the particular Account it is associated with. That is why you have the private Account account reference.
The getAccount() method gives you access to the customer's Account so you can work on the specific account associated with that customer. So, for example, if you would need to access the account I'd for a customer you would call;