+ 3

I have a problem with the output

Ecapsulation not working with output https://code.sololearn.com/ct4RP97e4GeE/?ref=app

27th Apr 2018, 5:38 PM
Evan Martine
5 Respuestas
+ 4
You need to do System.out.println()... As it is you are not printing anything to the screen... System.out.println(evan.viewBalance());
27th Apr 2018, 5:43 PM
cyk
cyk - avatar
+ 3
He wasn't trying to access a variable from outside the class. He just did not have a print statement, which was solved. He changed the code after we solved it so you cannot see the original error... And he does have a getter called viewBalance. And if the original balance is always 0, deposit can pass for a setter as well.
30th Apr 2018, 12:59 PM
cyk
cyk - avatar
+ 1
you can't access the variable from outside the class since its private , use getters and setters
30th Apr 2018, 12:11 PM
Joash N
Joash N - avatar
+ 1
class BankAccount { private double balance = 0; //get initial double in the balance public double getBalance(){ return balance ; } //set new value to the balance public voind setBalance(double balance){ this.balance=balance; } public void deposit(double x) { balance += x; } public double viewBalance(){ return balance; } } public class Program { public static void main(String[] args) { BankAccount evan = new BankAccount(); evan.deposit(40.25); evan.deposit(-40.25); System.out.println(evan.viewBalance()); } }
30th Apr 2018, 12:35 PM
Joash N
Joash N - avatar
0
SUBNETTING GUY Yep! Cyk helped me out there my problem was having the getter function in a system.out.print statement
30th Apr 2018, 5:49 PM
Evan Martine