0

I have a variable called "amount" in one of my methods but I need to make reference to it in another method. I'm not sure of the syntax used.

Here is the code:

public static void startup()
{
    String message = "Welcome to Toronto Mutual!";
    //prompts user to enter their employee ID
    String logIn = JOptionPane.showInputDialog("Please enter your employee `enter code here`ID.");
    int employeeID = Integer.parseInt(logIn);

    String input = JOptionPane.showInputDialog("Please enter the transaction `enter code here`type, bank ID and amount all separated by a comma.");
    input=input.toUpperCase();
    if (input.equals("END")){
        JOptionPane.showMessageDialog(null,"Goodbye.");
        System.exit(0);
    }
    else
    {
        int balance = 100;
        String []transaction = new String[3];
        transaction = input.split(",");
        String type = transaction[0]; 
        int bankID = Integer.parseInt(transaction[1]);
        type=type.toUpperCase();
... // there's more but it's irrelevant

How do I use the variable "amount" and "bankID" outside of the method?

3
  • I don't see amount in the code. Commented Apr 4, 2012 at 23:49
  • 1
    Don't see both "amount" and "bankID" in the code. But if you really need to reference it in another method, just initialize it outside this startup() method. private string amount; and do whatever you want with it in this method. You can read it from another method -- just make sure you're calling it in the correct order. Commented Apr 5, 2012 at 0:47
  • Or pass it as an argument to the method that needs it. Commented Jun 2, 2013 at 16:55

1 Answer 1

1

Two ways:

  1. Pass amount to the other method through argument

    private void otherMethod(int amount) {...}

  2. Create an instance variable so that you can access it within the class scope

    private int amount;

    If you go with this route, it is better to use getter and setter.

    public int getAmount() {
        return amount;
    }
    
    public void setAmount(int amount) {
        this.amount = amount;
    }
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.