1

I'm currently stuck on a project in my Intro to Java class and need any help I can get. I need to pass a parameter from my main method to a non-static method in another class and then print a double value from that class to the console.

When modeled as simply as possible, it looks like this:

public class UserAccount {

    double accountBalance = 0.0;

    public void addToBalance(double amountToAdd) {
        accountBalance = accountBalance + amountToAdd;
    }

    public double getBalance() {
        return accountBalance;
    }
}

and...

public class Driver {

    public static void main(String[] args) {
        System.out.println("Enter an amount to add to your balance:");
        Scanner sc = new Scanner(System.in);
        double input = sc.nextDouble();

        // I can't figure out these last two lines:

        UserAccount.addToBalance(input);
        System.out.println("Account Balance: " + UserAccount.getBalance());
    }
}

I feel like those last two lines, in theory, should allow me to pass the scanned input to the addToBalance() method and then display the balance to the console via the main method. However, neither line will work because they are both calling non-static methods.

Last time this happened, I just added static modifiers to the methods being called. However, my professor literally deducted 50 points (out of 100) for this because the methods listed in the specification didn't include static modifiers. So I guess I can't do that for some reason.

I basically was just wondering if there is any way to accomplish all of this without using static methods? Keep in mind that this is an intro course, so any possible solutions shouldn't be too complicated (but I'll take literally anything at this point). If anybody can help me, it would be greatly appreciated as I am in dire need of help right now.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jacob M
  • 147
  • 3
  • 10
  • https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html – shmosel Jun 07 '18 at 07:01
  • maybe `UserAccount account = new UserAccount();account.addToBalance(input);account.getBalance();`, you should pay more attention in your classes :). – Michal Jun 07 '18 at 07:01
  • This shouldn't even compile, as you didn't instantiate an `UserAccount` object. And if you do, this should work fine – DamCx Jun 07 '18 at 07:01
  • 1
    Also, class definition should not have (). – Rcordoval Jun 07 '18 at 07:03
  • @Michal thank you for your answer as I do appreciate it, however the class is online and the professor only leaves us with the textbook and class discussion forums. so it's not a matter of paying attention in class, but rather a scarcity of resources available to me at this time. so once again, i am grateful for your help, but i could do without your personal commentary. – Jacob M Jun 07 '18 at 07:12
  • @Rcordoval ah yes, sorry my mistake. the format in my actual source file is correct, but I accidentally included them when posting on here. I will edit this, thanks for pointing that out! – Jacob M Jun 07 '18 at 07:14

2 Answers2

1

In order to access non-static methods and variables you have to create an instance (object) of that class. Add this at the top of your main() method.

UserAccount u = new UserAccount ();

Now call,

u.addToBalance(input);
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
0

If the class is not static then you need to instantiate it

UserAccount ua = new UserAccount ();
ua.addToBalance(input);
System.out.println("Account Balance: " + ua.getBalance());
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64