Results 1 to 4 of 4
- 08-30-2009, 08:08 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 2
- Rep Power
- 0
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:
If I use these methods with a class named BankAccount, likeJava 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; }
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!Java Code:BankAccount.Create(Johnson, 3233, 100)
- 08-30-2009, 08:40 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
In some other class you can say:
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.Java Code:BankAccount ac = BankAccount.create(Johnson, 3233, 100); ac.deposit(100.00);
- 08-30-2009, 09:19 AM #3
Member
- Join Date
- Aug 2009
- Posts
- 2
- Rep Power
- 0
thanks, that's what I was looking for...
Thanks for the help.
- 08-30-2009, 10:18 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Similar Threads
-
how to create static array in class
By itaipee in forum New To JavaReplies: 1Last Post: 04-19-2009, 03:33 PM -
'Class' Object and calling Static Methods?
By mikeiz404 in forum Advanced JavaReplies: 3Last Post: 01-24-2009, 12:58 PM -
'Class' Object and calling Static Methods?
By mikeiz404 in forum New To JavaReplies: 2Last Post: 01-24-2009, 05:10 AM -
Abstract Class with Static Methods
By bugger in forum New To JavaReplies: 7Last Post: 09-05-2008, 12:20 AM -
Mocking static methods of class
By Kat in forum New To JavaReplies: 3Last Post: 11-08-2007, 12:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks