1180
private int accountNumber;
private double balance;
// constructor for BankAccount class
public BankAccount(String accountOwnerName, String accountOwnerSSN,
int accountNumber, double initialBalance, int initialRate)
{
this.accountOwnerName = accountOwnerName;
this.accountOwnerSSN = accountOwnerSSN;
this.accountNumber = accountNumber;
this.balance = initialBalance;
this.start(initialRate);
}
// start method will add interest to balance every 30 days
// creates timer object and interest adding action listener object
public void start(final double rate)
{
// anonymous inner class that implements the ActionListener interface
ActionListener adder = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
double interest = BankAccount.this.balance * rate / 100;
BankAccount.this.balance += interest;
}
};
Timer t = new Timer(1000 * 3600 * 24 * 30, adder);
t.start();
}
}
Example 4:
In the following Java example a simple applet provides the capability for a user to input a URL into
a text field and have the URL opened in a new browser window. The applet contains an inner class
that is an action listener for the submit button, when the user clicks the submit button the inner
class action listener's actionPerformed method will open the URL entered into the text field in a
new browser window. As with the previous examples using inner classes in this manner creates a
security risk by exposing private variables and methods. Inner classes create an additional security
risk with applets as applets are executed on a remote machine through a web browser within the
same JVM and therefore may run side-by-side with other potentially malicious code.
Example Language: (Bad)
public class UrlToolApplet extends Applet {
// private member variables for applet components
private Label enterUrlLabel;
private TextField enterUrlTextField;
private Button submitButton;
// init method that adds components to applet
// and creates button listener object
public void init() {
setLayout(new FlowLayout());
enterUrlLabel = new Label("Enter URL: ");
enterUrlTextField = new TextField("", 20);
submitButton = new Button("Submit");
add(enterUrlLabel);
add(enterUrlTextField);
add(submitButton);
ActionListener submitButtonListener = new SubmitButtonListener();
submitButton.addActionListener(submitButtonListener);
}
// button listener inner class for UrlToolApplet class
private class SubmitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == submitButton) {
String urlString = enterUrlTextField.getText();
URL url = null;
CWE Version 4.14
CWE-492: Use of Inner Class Containing Sensitive Data
CWE-492: Use of Inner Class Containing Sensitive Data