+ 1

Why does getters and setters don't work in sololearn

Guys I tried everything unlocking the private class .it's still say name is private and can not be accessed class Boy{ private int name=700; public int getName(){ return name; } public void setName(int other){ this.name=other; } } public class Program { public static void main(String[] args) { Boy obj=new Boy(); System.out.println(obj.name); } }

22nd Feb 2022, 3:50 PM
Tony Jadesola
1 ответ
+ 1
It's made private for a reason so that you don't access it directly with the name of the instance variable. System.out.println(obj.getName());
22nd Feb 2022, 3:54 PM
Avinesh
Avinesh - avatar