Results 1 to 2 of 2

Thread: i need help pls

  1. #1
    seun101 is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default i need help pls

    public class Account extends javax.swing.JFrame {

    /** Creates new form Account */
    public Account() {
    initComponents();
    setTitle("Bank Account");
    jTextArea1.setEditable(false);
    }



    /**
    * @param args the command line arguments
    */

    private int accountNumber =0;
    private double balance =0;


    public Account(int accNum, double bal)
    {
    accountNumber = accNum;
    balance = bal;
    }

    }
    public void deposit(double amount)
    {
    balance+=amount;
    }


    private void buttonCreateAccount(java.awt.event.ActionEvent evt) {

    Account newAcc = new Account(1234, 20000.0);
    jTextArea1.setText("Account Created \nAccount Number ="+newAcc.ID+", Account Balance= "+newAcc.balance);
    }


    private void buttonDeposit(java.awt.event.ActionEvent evt) {
    /*i am trying to do : newAcc.deposit(5000.0);
    jTextArea1.setText("new balance : "+balance);*/

    /*but newAcc can't call the method deposit
    i want it so that each time buttonDeposit is clicked, 5000 is added to the initial balance, which is 20000;

    please help, i'm a newbie to java */

    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    10

    Default

    newAcc is local to the buttonCreateAccount method. You have to make it a class variable if you want it to be able to accessed by all methods in the class.

    Read more: Intro to OOP: Java scope rules + Scope (The Java™ Tutorials > Java Naming and Directory Interface(TM). > Naming and Directory Operations)

Posting Permissions

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