Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why public constructor should be provided in javabean class

Tags:

java

javabeans

I heared rules of JavaBean, in which first and main rule is , for every JavaBean class explicitly programmer should provide public default constructor . Please can anyone explain why do we need to provide default constructor for JavaBean

UPDATE :

Please explain clearly, why jvm will not provide default constructor for JavaBeans and how jvm reacts on providing default constructor

like image 856
developer Avatar asked Feb 22 '12 11:02

developer


People also ask

Why constructor should be public in Java?

You make a constructor public if you want the class to be instantiated from any where. You make a constructor protected if you want the class to be inherited and its inherited classes be instantiated.

What is the purpose of a JavaBean?

JavaBeans is a portable, platform-independent model written in Java Programming Language. Its components are referred to as beans. In simple terms, JavaBeans are classes which encapsulate several objects into a single object. It helps in accessing these object from multiple places.

Should the constructor be public or private?

A constructor is a special member function of a class which initializes objects of a class. In C++, constructor is automatically called when object of a class is created. By default, constructors are defined in public section of class.

What is JavaBean class?

JavaBeans are classes that encapsulate many objects into a single object (the bean). It is a java class that should follow following conventions: Must implement Serializable. It should have a public no-arg constructor. All properties in java bean must be private with public getters and setter methods.


1 Answers

I heared rules of JavaBean, in which first and main rule is , for every JavaBean class explicitly programmer should provide public default constructor . Please can anyone explain why do we need to provide default constructor for JavaBean

JavaBean instances are created by reflective calls to the no-arg constructor. So there has to be such a constructor.

Please explain clearly, why jvm will not provide default constructor for JavaBeans and how jvm reacts on providing default constructor

The jvm will provide a default constructor for a JavaBean if you have explicitly provided no constructors. If you do provide a constructor, you must provide a no-arg constructor besides any that you define with parameters.

like image 78
Don Roby Avatar answered Oct 20 '22 00:10

Don Roby