+ 3
I have a problem with the output
Ecapsulation not working with output https://code.sololearn.com/ct4RP97e4GeE/?ref=app
5 Answers
+ 4
You need to do System.out.println()... As it is you are not printing anything to the screen...
System.out.println(evan.viewBalance());
+ 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.
+ 1
you can't access the variable from outside the class since its private , use getters and setters
+ 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());
}
}
0
SUBNETTING GUY Yep! Cyk helped me out there my problem was having the getter function in a system.out.print statement