aspose file tools*
The moose likes Beginning Java and the fly likes Instance variable of type some class in another class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instance variable of type some class in another class" Watch "Instance variable of type some class in another class" New topic
Author

Instance variable of type some class in another class

Vicky Chand
Greenhorn

Joined: Dec 10, 2008
Posts: 8
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

Joined: Jun 13, 2007
Posts: 6
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

Joined: Oct 21, 2008
Posts: 43
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

Joined: Dec 10, 2008
Posts: 8
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

Joined: Oct 13, 2005
Posts: 38412
    
  23
"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

Joined: Jan 28, 2003
Posts: 4179
    
  21

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;


Steve
Vicky Chand
Greenhorn

Joined: Dec 10, 2008
Posts: 8
Thank's got it
 
 
subject: Instance variable of type some class in another class