Win a copy of Spark in Action this week in the Open Source Projects forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

beans question (beginner) concept

 
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
 
Marshal
Posts: 67413
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A servlet is not a bean.

And variables should start with lowercase.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • 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
Marshal
Posts: 67413
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Hibernate Eclipse IDE Chrome
  • 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
Marshal
Posts: 67413
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice explanation Bear, cheers.
 
Bear Bibeault
Marshal
Posts: 67413
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • 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?
 
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
Hibernate Eclipse IDE Chrome
  • 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
Marshal
Posts: 67413
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • 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.
 
Our first order of business must be this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic