the use of private keyword
Posted
by LAT
on Stack Overflow
See other posts from Stack Overflow
or by LAT
Published on 2010-06-02T03:26:41Z
Indexed on
2010/06/02
3:33 UTC
Read the original article
Hit count: 177
java
Hi everyone
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 , getter and setter to access it. Why not put everything in public since we use it anyway.
public class BadOO {
public int size;
public int weight;
...
}
public class ExploitBadOO {
public static void main (String [] args) {
BadOO b = new BadOO();
b.size = -5; // Legal but bad!!
}
}
I found some code like this, and i saw the comment legal but bad. I don't understand why, please explain me.
© Stack Overflow or respective owner