• Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting up JavaBeans and Java Classes  RSS feed

 
Steve Wilson
Greenhorn
Posts: 4
Eclipse IDE jQuery Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to java web development and I am currently going through a book called "Java Servlets and JSP" by Murach. I have just be introduced to using JavaBeans and the author has changed the way he does a few things when he introduced Beans and set up the first bean.

We set up a User class as follows



Which is pretty easy for me to follow. Set up an object, constructors, initialize the variables, and create getters/setters and so on. This is similiar to the way I learned in some programming classes I took in college. Once I got the the JavaBean chapter the author set up the same User class but in a different manner:



The author decided to not use "this.firstName = firstName" any longer and uses "firstName = f" and changed the names of the Strings that the setters get passed and also changed the argument names for User as well. Now the code works just the same (I believe) but I am wondering why he chose to change his approach. The book states that to qualify as a JavaBean it must have a constructor which accepts no arguments, the Bean cant have any public variables, and the Bean must have get and set methods for all properties needed by JSPs. Is there another difference in the way JavaBeans are set up? The renaming is causing me confusion.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Wilson wrote:
The author decided to not use "this.firstName = firstName" any longer and uses "firstName = f" and changed the names of the Strings that the setters get passed and also changed the argument names for User as well. Now the code works just the same (I believe) but I am wondering why he chose to change his approach. The book states that to qualify as a JavaBean it must have a constructor which accepts no arguments, the Bean cant have any public variables, and the Bean must have get and set methods for all properties needed by JSPs. Is there another difference in the way JavaBeans are set up? The renaming is causing me confusion.

As far as codes are concerned, both are same. Just have two different style which doesn't matter really. A JavaBean is just an standard which requires
1. No argument constructor
2. All properties are declare private with accessible as getter/setter property
3. implements serializable interface.

As far as these three properties are provided, it is fine irrespective of any styles developer choose.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

There are certain naming conventions for methods in a Bean.
Do Beans really have to be Serializable?

The difference between the style with this. in setters and the style with different names is that you are exposing the better name to users. Seeing a method in the documentation, it should be obvious that first means first, but f is not so obvious.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!