Results 1 to 4 of 4
  1. #1
    Pilot Ace is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Default Static Builder and Create Methods for simple class

    I was told by my instructor to include a static builder method and static create method as part of good class design in all my programs, the problem is, I don't understand how I would use other non-static methods associated with the class. Here are the two methods:

    Java Code:
    public static BankAccount Create(String name, int accountNumber, double balance)           
        {
            BankAccount anAccount = null;
            anAccount = new BankAccount(name, accountNumber, balance);
            return anAccount;
        }
    
    public static BankAccount Builder()    
        {
            BankAccount anAccount = new BankAccount();
            return anAccount;
        }
    If I use these methods with a class named BankAccount, like

    Java Code:
    BankAccount.Create(Johnson, 3233, 100)
    how (if it is even possible) can I use non-static methods like 'withdraw' or 'deposit' to modify the balance or other instance variables? The static methods create a new account, 'anAccount', but I cannot use non-static methods with anAccount.withdraw. I simply just don't understand what the purpose of these static methods in a class are if non-static methods cannot be used. Please help!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    In some other class you can say:

    Java Code:
    BankAccount ac = BankAccount.create(Johnson, 3233, 100);
    ac.deposit(100.00);
    Of course the reference ac has to be stored somewhere - like a list of bank accounts - otherwise it will no longer be accessible when the method that called create() has ended.

  3. #3
    Pilot Ace is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Default thanks, that's what I was looking for...

    Thanks for the help.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

Similar Threads

  1. how to create static array in class
    By itaipee in forum New To Java
    Replies: 1
    Last Post: 04-19-2009, 04:33 PM
  2. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum Advanced Java
    Replies: 3
    Last Post: 01-24-2009, 01:58 PM
  3. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 06:10 AM
  4. Abstract Class with Static Methods
    By bugger in forum New To Java
    Replies: 7
    Last Post: 09-05-2008, 01:20 AM
  5. Mocking static methods of class
    By Kat in forum New To Java
    Replies: 3
    Last Post: 11-08-2007, 01:24 AM

Posting Permissions

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