Results 1 to 5 of 5
- 04-30-2008, 04:30 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 3
- Rep Power
- 0
Calling a method for all instances of an object
Hi
given the above and my BankAccount class has a method getDetails() which prints a formatted list of vars for the account object.Java Code:import java.lang.*; import java.util.*; public class BankManagement{ public static void main(String[] args) { //vars, objects // construct instances of BankAccount and pass initial account information // account-no First Last Type balance credit BankAccount acc01 = new BankAccount(3856296735L,"Jenny","Popdeck","savings",4820.88,-50); BankAccount acc02 = new BankAccount(3478957793L,"Alex","Fryhall","credit",11899,-10000); BankAccount acc03 = new BankAccount(3709409798L,"Joey","Boilcraft","check",333.60,0); } }
Please suggest a way of running the method for all instances of the BankAccount object which would essentially be
but automaticJava Code:acc01.getDetails();acc02.getDetails();acc03.getDetails();
thanks in advance for your considerate help
D
mickLast edited by rattle; 04-30-2008 at 04:34 AM. Reason: typos typos typos!!
- 04-30-2008, 04:37 AM #2
use a for loop
for(int i = 0; i < myArray.length; i++)
{
myArray[i].getDetails;
}My IP address is 127.0.0.1
- 04-30-2008, 04:49 AM #3
Member
- Join Date
- Apr 2008
- Posts
- 3
- Rep Power
- 0
the bit i don't get is how to get all the instance names into an array in one statement.
Last edited by rattle; 04-30-2008 at 04:51 AM.
- 04-30-2008, 06:21 AM #4
Member
- Join Date
- Apr 2008
- Posts
- 3
- Rep Power
- 0
and even when i wack the instance names into an array manually, it spits out"
Java Code:import java.lang.*; import java.util.*; public class BankManagement{ public static void main(String[] args) { //vars, objects String temp=""; // construct instances of BankAccount and pass initial account information // account-no First Last Type balance credit BankAccount acc01 = new BankAccount(3856296735L,"Jenny","Popdeck","savings",4820.88,-50); BankAccount acc02 = new BankAccount(3478957793L,"Alex","Fryhall","credit",11899,-10000); BankAccount acc03 = new BankAccount(3709409798L,"Joey","Boilcraft","check",333.60,0); String[] accArray={"acc01", "acc02", "acc03"}; for(int i = 0; i < accArray.length; i++){ accArray[i].getAccountDetails(); } } }Java Code:C:\Documents and Settings\User\My Documents\Java\BankManagement.java:20: cannot find symbol symbol : method getAccountDetails() location: class java.lang.String accArray[i].getAccountDetails(); ^ 1 error
- 04-30-2008, 02:10 PM #5
Try this:
Java Code:BankAccount [] accArray={acc01, acc02, acc03};Daniel @ [www.littletutorials.com]
Language is froth on the surface of thought
Similar Threads
-
Object Reflection: Creating new instances
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:13 PM -
Object from String (calling method dynamically)
By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 09:22 PM -
Naming object instances
By oldgit in forum New To JavaReplies: 9Last Post: 02-07-2008, 11:18 PM -
method calling?
By frejon26 in forum New To JavaReplies: 4Last Post: 01-25-2008, 03:38 AM -
Help with Calling a method
By Albert in forum New To JavaReplies: 3Last Post: 07-10-2007, 03:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks