• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why can a java bean must only blank or no arg constructor ?  RSS feed

 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per java or jsp spec java beans or just your regular java objects should have only a no arg constructor. Properties can only be set by getters and setters.
What is the reason for putting this restriction ?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ali Gordon wrote:As per java or jsp spec java beans or just your regular java objects should have only a no arg constructor. Properties can only be set by getters and setters.
What is the reason for putting this restriction ?


Hi Ali, when you say 'java beans or just regular java objects' you should realize that they are different things (not all 'regular java objects' need to follow Java Beans specs, just those that are intended to be used as Java Beans). The Java Beans specification is really just a set of un-enforced rules to make it so your classes can be automatically created and used by tools that know about the specification. The Java Beans spec says there has to be a no argument constructor so that the tools that need to automatically create an instance of the Java Bean can do so, without complex mechanisms to figure out what parameters need to be passed and how to gain access to those parameters. The rules for the getter and setter methods are similarly pragmatic. A tool that uses Java Beans needs to be able to find out what properties are available, and needs to be able to find the correct mutator and accessor methods. The Java Beans standard makes that easy as just parsing the names of the methods, otherwise you would need complex configuration files or helper classes, or some sort of inheritance trick. Nowadays you could use annotation but when the Java Beans spec was created I don't think annotations were around yet.

So the rules are all about making it easy for the tools which use Java Beans have a manageable time knowing how to use the Java Beans without a lot of configuration.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, to summarize: predictable behavior.
 
Anagha Khyathi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java provides a default no-argument constructor if no other constructors are defined. Thus, if you have other constructors you must explicitly define a no-arg constructor.

These frameworks use the reflection API and look at method names to determine how to set properties. The arguments of a constructor can only be found by type, not by name, so there is no way for the framework to reliably match properties to constructor arguments. Therefore, they require a zero-arg constructor to create the object, then can use the setter methods to initialize the data.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some tools can use the ConstructorProperties annotation to use constructors with arguments. That has been available since Java6.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!