aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why we use java beans?" Watch "Why we use java beans?" New topic
Author

Why we use java beans?

Abhinaba Basu
Greenhorn

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

e.g
<code>
//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();

mybean.setRollNo(1233);
int rollNo = mybean.getRollNo();

</code>

But instead of that if write the code ike this:

<code>
public class MyBean{
public int rollNo;

}

// and can access like this:

MyBean mybean = new MyBean();

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

</code>

Why we dont do this?

Pleas expplain...


<b>Abhinaba basu</b><br />Software Engineer,<br /><a href="http://"http://www.kaizen-it.com"" target="_blank" rel="nofollow">Kaizen Informatics Pvt Ltd</a><br /><a href="http://"http://engineers.bizhat.com"" target="_blank" rel="nofollow">My site</a> | <a href="http://"http://theabhinaba.bizhat.com"" target="_blank" rel="nofollow">My Blog</a> | <a href="http://"http://www.kaizen-it.com"" 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,
Justin


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.

e.g
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: http://aspose.com/file-tools
 
subject: Why we use java beans?
 
Similar Threads
write() v/s println()
Urgent: How to use Indexed Properties
How to write a custom alert window?
stupid Microsoft -- IE 6.0 on XP trouble . . .
Why ModelDriven not working?