Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questions about java bean

 
Adnan Gondal
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!

I am having two questions, kindly answer them.

Q1: Can we have data members with default access modifier in a java Bean?

Q2: Is it must to provide getter and setter methods for all the properties of a java bean or we can skip one if we don't need it?
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Adnan,

Surely we can have data member with default access / public access in the Java Bean. But it's good practice to have data member as Private and get them access by getter and setter methods.

also you need not to make getter and setter method for each of the data member. But if you dont have a getter /setter method for a private data member you won't be able to access that data member.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48930
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea of a bean is that it has a no-arguments public constructor and public get and set methods for every field. That way it is possible to write the state of the class into an XML file and reconstruct it later.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic