Results 1 to 5 of 5
  1. #1
    rattle is offline Member
    Join Date
    Apr 2008
    Posts
    3
    Rep Power
    0

    Default Calling a method for all instances of an object

    Hi
    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);
    
    
    	}
    }
    given the above and my BankAccount class has a method getDetails() which prints a formatted list of vars for the account object.

    Please suggest a way of running the method for all instances of the BankAccount object which would essentially be

    Java Code:
    acc01.getDetails();acc02.getDetails();acc03.getDetails();
    but automatic


    thanks in advance for your considerate help D
    mick
    Last edited by rattle; 04-30-2008 at 04:34 AM. Reason: typos typos typos!!

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    use a for loop

    for(int i = 0; i < myArray.length; i++)
    {
    myArray[i].getDetails;
    }
    My IP address is 127.0.0.1

  3. #3
    rattle is offline Member
    Join Date
    Apr 2008
    Posts
    3
    Rep Power
    0

    Default

    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.

  4. #4
    rattle is offline Member
    Join Date
    Apr 2008
    Posts
    3
    Rep Power
    0

    Default

    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

  5. #5
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    Try this:

    Java Code:
    BankAccount [] accArray={acc01, acc02, acc03};
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

Similar Threads

  1. Object Reflection: Creating new instances
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:13 PM
  2. Object from String (calling method dynamically)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 09:22 PM
  3. Naming object instances
    By oldgit in forum New To Java
    Replies: 9
    Last Post: 02-07-2008, 11:18 PM
  4. method calling?
    By frejon26 in forum New To Java
    Replies: 4
    Last Post: 01-25-2008, 03:38 AM
  5. Help with Calling a method
    By Albert in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •