Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instance variable of type some class in another class

 
Vicky Chand
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey this my first time here. i was doing bankprj thing from java SL-275 lab book where i have a class Customer which goes something like this(MY QUESTIONS ARE RIGHT NEXT TO MY DOUBTS IN UPPERCASE

public class Customer {
private String firstName;
private String lastName;
private Account account; //THIS IS THE INSTANCE VARIABLE OF TYPE ACCOUNT

public Customer(String f, String l){
firstName = f;
lastName = l;
}
public String getFirtName(){
return firstName;
}
public String getLastName(){
return lastName;
}

//WHAT IS THE USE OF THE FOLLOWING CODE AND HOW DO I USE IT IN MY MAIN METOHD
public Account getAccount(){
return account;

}
public void setAccount(Account acct ){
account = acct ;
}

}
 
Gautam Sathe
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vinay C:

private Account account; //THIS IS THE INSTANCE VARIABLE OF TYPE ACCOUNT


//WHAT IS THE USE OF THE FOLLOWING CODE AND HOW DO I USE IT IN MY MAIN METOHD
public Account getAccount(){
return account;

}
public void setAccount(Account acct ){
account = acct ;
}

}


- 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.

Hope it help.

Gautam.
 
Brian Lang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

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.
 
Vicky Chand
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Vinay C" Please read the important administrative private message which I have just sent you.

Please read our FAQ and don't write all uppercase.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Vicky Chand
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank's got it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic