File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Why we use java beans? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why we use java beans?" Watch "Why we use java beans?" New topic

Why we use java beans?

Abhinaba Basu

Joined: Jun 06, 2006
Posts: 15
My question actually like this: Why we use seters and geters instead of create public variable?

//A bean

public class MyBean{
private int rollNo;

public void setRollNo(int rollNo){
this.rollNo = rollNo;

public int getRollNo(){
return rollNo;


// And we can access this by given code:

MyBean mybean = new MyBean();

int rollNo = mybean.getRollNo();


But instead of that if write the code ike this:

public class MyBean{
public int rollNo;


// and can access like this:

MyBean mybean = new MyBean();

mybean.rollNo = 1233;
int rollNo = myBean.rollNo;


Why we dont do this?

Pleas expplain...

<b>Abhinaba basu</b><br />Software Engineer,<br /><a href="http://""" target="_blank" rel="nofollow">Kaizen Informatics Pvt Ltd</a><br /><a href="http://""" target="_blank" rel="nofollow">My site</a> | <a href="http://""" target="_blank" rel="nofollow">My Blog</a> | <a href="http://""" target="_blank" rel="nofollow">My office</a>
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
you don't want a classes variables to be public, you always make them private, not doing this is called "class protocol violation",

it's just better technique to create methods to do the work for you.

anyways, the client should never be able to directly modify the driver classes variables..

Hope this helps,

You down with OOP? Yeah you know me!
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132
If you declare properties to access your private variables, you have more control to validate the values users are trying to assign to your variables.

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
And don't automatically make a getter & setter for every field, unless you're in a specific situation like a Data Transfer Object where that's the whole point. Most objects are better with a "tell, don't ask" design. Don't ask an object what all it's fields are, just tell it what to do with those fields. Often that means putting more code into the class instead of outside. It might be a theoretical ideal if an object has all private data and only "behavior" methods that do useful things for you.

Kent Beck says it's a warning sign if you get two or more fields from an object and do something with them. It's an even bigger warning if you put the result back into the object. See the entertaining Knight's Principles for examples.
[ July 20, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Why we use java beans?
It's not a secret anymore!