How to write methods in utility classes that you can then call from any other class.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on Utility classes, JAR files, and JAVA beans in the Programmer's Guide |
To create a class whose methods can be called by all classes:
As with all external methods, you must import the BOLoanCalculator class to be able to call its methods. The following code calls the BOLoanCalculator's calculateLoanPayment()
to compute the loan payment, given the following information, which is provided by the user:
private void handle_btnCalculateNetPay_actionPerformed(ActionEvent evt) { // This example illustrates calling a method in a utility object BOLoanCalculator boLoanCalculator = new BOLoanCalculator(); double dbAmount = fldAmount.getValue(); double dbAPR = fldAPR.getValue(); int iNumberPayments = fldNumberPayments.getValue(); double dbPayment = boLoanCalculator.calculateLoanPayment( dbAmount, dbAPR, iNumberPayments); fldPaymentAmount.setValue(dbPayment); }
fldAmount.getValue()
gets the user-entered value from the Enter loan amount text field. Similarly, fldAPR.getValue()
and fldLength.getValue()
get the user-entered APR and number-of-payments values from their respective text fields.
calculateLoanPayment()
. This is a BOLoanCalculator method. It returns the payment amount, which you will display in a text field.
fldPaymentAmount.setValue()
and supply the computed result as its parameter.