Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

beans question (beginner) concept  RSS feed

 
william chaple
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all. I am trying to understand the concept so I was hoping someone can provide me with the solution of... ( i know how to code it but i am trying to understand the concept behind it)


which one is a bean?




or this one?







thats all im just trying to see the difference
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A servlet is not a bean.

And variables should start with lowercase.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RequestData is a bean and along with Bear's advice, the member variables hobby and aversion should be private.
 
william chaple
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool! Thanks guys!

Oh yeah, and i forgot about the uppercase, meant to make it lower
 
william chaple
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so when I ask a question, lets say (concept wise, I am horrible I know lol, just trying to learn its vocab and stuff)

Write the code for a Java bean that stores the data for an application that has input elements named first, middle and last.

specifically "write a code for a java bean"

is that just saying write a java bean that has the 3 attributes/parameters first middle and last?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
william chaple wrote:is that just saying write a java bean that has the 3 attributes first middle and last?

That's how I would interpret it.

And, the correct term is property, not attribute.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear

Do you use the term property? I much prefer member variable. Isn't property more of a C# term?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, a variable is a very specific thing.

The items that a bean exposes though it accesors and mutators are its properties. It has nothing to do with the actual instance variables.

Whether properties are backed by instance variables or not is opaque to the user of the bean. I've written many beans where some of the properties are not stored in variables.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice explanation Bear, cheers.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, consider this bean:


The variables are a and b. The properties are fred and barney.

Disclaimer: a and b are not acceptable variable names. They are just used here to illustrate that the instance variables are independent of the properties.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote: I've written many beans where some of the properties are not stored in variables.


I see and understand your example, but I don't understand the advantage of this technique. Why would you do it this way? What are you gaining?
 
Marshall Blythe
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:
I see and understand your example, but I don't understand the advantage of this technique. Why would you do it this way? What are you gaining?


You may encounter scenarios where some properties can be derived from existing variables or from expressions. Take this example of a Person class:



This class has a birthDate property implemented as a variable named birthDate, but it also has an ageInYears property that is derived from a calculation involving the current date and the birthDate. Derived properties like this are usually read-only.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:I see and understand your example, but I don't understand the advantage of this technique. Why would you do it this way? What are you gaining?

Let's look at Marshall's example and say you didn't have the ageInYears property.

Now, lets suppose a class, ClassA, needs to calculate the person's age:

Now, lets suppose another class, ClassB, also needs to perform the same calculation:

Now, lets suppose a multitude of other classes need to perform the same calculation. It is at this point alarm bells should be ringing about the violation of the DRY principle.

Sure, you could refactor the logic to calculate the person's age into some utility class that is invoked by all classes that need it.

However, such utility classes are not very OO. One of the fundamentals of OO is encapsulation which is to group (or encapsulate) data and its behaviour together. With this in mind, the data is the birth date of a person and the behaviour is the age in years calculation which should lead a developer to put these together in the same class.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:
Bear Bibeault wrote: I've written many beans where some of the properties are not stored in variables.


I see and understand your example, but I don't understand the advantage of this technique. Why would you do it this way? What are you gaining?


In such cases the value of the property are computed from internal values. There's no need to store the computed values.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thanks for the explanation. Now I know to watch out for scenarios like that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!