• Post Reply Bookmark Topic Watch Topic
  • New Topic

About Default Constructor in a Bean  RSS feed

 
Naresh Shanmugam
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I knew the default constructor is the no-arg constructor that is automatically provided by the compiler when the source code doesn't contain an explicit constructor. If recently learnt that a Bean(simple java bean) must contain a default constructor.
I wondering what these default constructor are gonna do behind the screen, Am i missing any tricky part here?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allow an instance to be created without args.
 
Naresh Shanmugam
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So a bean's Instance variables must be initialized only through the setters and not through constructor thats the reason to have a public no arg constructor in a bean. But a POJO can have a parameterized constructor. Is my understanding correct? Please correct me if not?
 
Naresh Shanmugam
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranchers can anybody confirm Please,

Is this understanding correct?

1. A bean's Instance variables must be initialized only through the setters and not through constructor thats the reason to have a public no arg constructor in a bean.

2. A POJO can have a parameterized constructor and it must not implement any interface or it must subclass anyother class
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult for "beginning Java". Moving thread.
 
subhash kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whenever beans are used to carry data as they are meant for,
it must have no arguments constructor or default constructor, because that is the way to create a blank bean(not carrying any data)
other advantage is , if any application or tool or framework using your bean it will be expacting bean with the no argument constructor or default constructor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!