I am new to programming. I am learning Java now, there is something I am not really sure, that the use of private. Why programmer set the variable as private then write , ge
It's a common pet-peeve of many programmers - Java code with private fields and public accessors and mutators. The effect is as you say, those fields might as well been public.
There are programming languages that voice for the other extreme, too. Look at Python; just about everything is public, to some extent.
These are different coding practices and a common thing programmers deal with every day. But in Java, here's my rule of thumb:
public.private. Provide a getter if you want read access, and provide a setter if you want write access.return - for example, if you want to track the number of times an attribute is read by other programs during an object's life time.That's just a brief overview on access levels. If you're interested, also read up on protected access.