instance variables?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Tuesday, February 28, 2006 03:51 AM

why a bean class should not have public instance variables?

Re: instance variables?

Posted By:   Anonymous  
Posted On:   Tuesday, February 28, 2006 05:37 AM

Information hiding - have a private variable and public getter/setter. So you have a better control doing the right and having derived values.

public class Square {
public int width;
public int height;
public int size;
}

mySquare.width = -1;

public class Square {
private int width;
private int height;

public int getWidth() { return width; }
public int getHeight() { return height; }

public void setWidth(int w) {
if (w>=0) width = w;
}
public void setHeight(int h) {
if (h>=0) height = h;
}

public int getSize() {
return width * height;
}
}
About | Sitemap | Contact