1

I know

A JavaBean is just a standard

All properties private (use getters/setters)
A public no-argument constructor
Implements Serializable.

Source

We all know it is not required to provide a non argument constructor in a class, because if we have not specified any constructor in our class java compiler will create a non argument constructor. If so why programmers wanted to create a non argument constructor in a javabean as a convention.

Community
  • 1
  • 1
gihanmu
  • 136
  • 1
  • 12
  • 3
    So tools and libraries could instantiate bean instances with no user intervention or default values required. – Dave Newton Oct 25 '14 at 10:11

5 Answers5

2

It is considered good practice by some to always include the non-arg constructor in your code, because that prevents the scenario where a later maintenance introduces another constructor, thereby discarding the implicit non-arg one, thereby breaking any external code that relies on it.

Erwin Smout
  • 18,113
  • 4
  • 33
  • 52
  • Yes the point is clear, because someday if somebody inserts a constructor in the class which has some arguments, the compiler will no longer create its default constructor – gihanmu Oct 25 '14 at 10:18
2

You are confusing requirements on the JavaBean class with the requirements on its source code. No part of the JavaBeans specification deals with how your source code must look like, it is strictly about the resulting class.

So yes, each JavaBeans class must provide a nullary constructor, and how you achieve that with source code (or even with a bytecode generator) is completely up to you.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • I think what you are saying is Java does not care who creates the default constructor. It may be the programmer or the compiler. All it wants is to have a default constructor – gihanmu Oct 25 '14 at 10:21
  • Exactly, it is about the API of the class as seen by other classes and not about the technicalities of how that API comes about. – Marko Topolnik Oct 25 '14 at 10:22
  • Thanks Marko. I love this answer and accepted it. Thanks for other answers too – gihanmu Oct 25 '14 at 10:24
0

You don't have to create it explicitly. There's no rule saying you have to do that. Even for a JavaBean, it's fine to leave the compiler to create one for you (as long as you're not providing another one, in which case you'd need an explicit no-arg constructor too).

But there does need to be one, explicit or implicit, because the ORM needs to be able to create instances.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
  • ...except in the cases in which you provide your own constructor. Then, you're kind of stuck. – Makoto Oct 25 '14 at 10:10
  • @Makoto yes, I've clarified. I'd meant that it's fine to let the compiler do it in cases where the compiler will do it... (the point being that there has to be one somehow). – chiastic-security Oct 25 '14 at 10:12
0

Without one many API internals like ORMs or IOC containers can't instantiate the object in order to proceed with setting the bean properties from the data source or other bean dependencies.

Many do approximately this:

Class<?> clazz = Class.forName("com.foo.BeanClass");
Constructor<?> constructor = clazz.getConstructor();
Object bean = constructor.newInstance();
codenheim
  • 20,467
  • 1
  • 59
  • 80
  • @MarkoTopolnik, I guess you mean "does *not*". But... it's actually untrue. The first non-argument c-tor of the first non-serializable class is to be invoked, more like the "" method the c-tor designates. An example: http://pastebin.com/SYgFvZTh – bestsss Oct 26 '14 at 19:02
  • @bestsss Didn't know that specific detail... so, even though we may not need any constructors in our classes (they can all be serializable), the Object constructor is still involved. – Marko Topolnik Oct 26 '14 at 20:27
  • @MarkoTopolnik, yes the normal case (where the first subclass of Object is already serializable) is invoking of Object c-tor. The need to call the c-tor on non-Externalizable classes arises from the fact that super class fields have to be inited (incl. the final fields) somehow. In the end even in byte code there are no real c-tors, the object is created uninitialzed entirely and then some method is invoked. The verifier ensures no meddling in normal classes but the serialization is sidestepping that part. – bestsss Oct 26 '14 at 20:49
0

You'd want to create a no argument in these cases:

1) You want to do some logic in the no argument constructor, so can't use the default.

2) You have other constructors that take arguments, in that case no default no-arg constructor will be provided for you.

point 2 implies that having an explicit no arg constructor to start with allows you to add future constructors with arguments without worrying about losing the implicit no-arg constructor.

Andrew Luo
  • 919
  • 1
  • 5
  • 6